URL.createObjectURL() 静态方法会创建一个 DOMString,其中包含一个表示参数中给出的对象的URL。这个 URL 的生命周期和创建它的窗口中的 document 绑定。这个新的URL 对象表示指定的 File 对象或 Blob 对象。

Note: 此特性在 Web Worker 中可用。

注意:此特性在 Service Worker 中不可用,因为它有可能导致内存泄漏。

语法

objectURL = URL.createObjectURL(object);

参数

object
用于创建 URL 的 File 对象、Blob 对象或者 MediaSource 对象。​

返回值

A DOMString containing an object URL that can be used to reference the contents of the specified source object.

示例

查看使用对象URL显示图片.

附注

内存管理

在每次调用 createObjectURL() 方法时,都会创建一个新的 URL 对象,即使你已经用相同的对象作为参数创建过。当不再需要这些 URL 对象时,每个对象必须通过调用 URL.revokeObjectURL() 方法来释放。浏览器会在文档退出的时候自动释放它们,但是为了获得最佳性能和内存使用状况,你应该在安全的时机主动释放掉它们。

规范

规范 状态 备注
File API
createObjectURL()
Working Draft Initial definition.
Media Source Extensions
URL
Recommendation

MediaSource extension.

Older versions of this specification used createObjectURL() for MediaStream objects; this is no longer supported.

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
createObjectURL
Experimental
Chrome Full support 8Edge Full support 12Firefox Full support 19
Notes
Full support 19
Notes
Notes createObjectURL() is no longer available within the context of a ServiceWorker.
IE Full support 10Opera Full support 15Safari Full support 6WebView Android Full support YesChrome Android Full support 18Firefox Android Full support 19
Notes
Full support 19
Notes
Notes createObjectURL() is no longer available within the context of a ServiceWorker.
Opera Android Full support 14Safari iOS Full support 6Samsung Internet Android Full support Yes
No longer accepts MediaStream objectChrome ?
Notes
?
Notes
Notes See here for progress on deprecation.
Edge ? Firefox Full support 62IE ? Opera ?
Notes
?
Notes
Notes See here for progress on deprecation.
Safari ?
Notes
?
Notes
Notes See here for progress on deprecation.
WebView Android ?
Notes
?
Notes
Notes See here for progress on deprecation.
Chrome Android ?
Notes
?
Notes
Notes See here for progress on deprecation.
Firefox Android Full support 62Opera Android ?
Notes
?
Notes
Notes See here for progress on deprecation.
Safari iOS ?
Notes
?
Notes
Notes See here for progress on deprecation.
Samsung Internet Android ?

Legend

Full support  
Full support
Compatibility unknown  
Compatibility unknown
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
See implementation notes.
See implementation notes.

相关链接

文档标签和贡献者

标签: 
最后编辑者: RainSlide,