URL.createObjectURL() static method creates a DOMString containing a URL representing the object given in the parameter. The URL lifetime is tied to the document in the window on which it was created. The new object URL represents the specified File object or Blob 对象。

To release an object URL, call revokeObjectURL() .

注意: 此特征可用于 Web 工作者 .

注意: This feature is not available in 服务工作者 due to its potential to create memory leaks.

句法

const objectURL = URL.createObjectURL(object)
				

参数

对象
File , Blob ,或 MediaSource object to create an object URL for.

返回值

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

范例

Using object URLs to display images .

用法注意事项

内存管理

每次调用 createObjectURL() , a new object URL is created, even if you've already created one for the same object. Each of these must be released by calling URL.revokeObjectURL() when you no longer need them.

Browsers will release object URLs automatically when the document is unloaded; however, for optimal performance and memory usage, if there are safe times when you can explicitly unload them, you should do so.

Using object URLs for media streams

In older versions of the Media Source specification, attaching a stream to a <video> element required creating an object URL for the MediaStream . This is no longer necessary, and browsers are removing support for doing this.

重要: If you still have code that relies on createObjectURL() to attach streams to media elements, you need to update your code to simply set srcObject MediaStream 直接。

规范

规范 状态 注释
文件 API
The definition of 'createObjectURL()' in that specification.
工作草案 初始定义。
媒体源扩展
The definition of 'URL' in that specification.
推荐

MediaSource extension.

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

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
createObjectURL Chrome 19 Edge 12 Firefox 19
19
createObjectURL() is no longer available within the context of a ServiceWorker .
IE 10
10
If the underlying object does not have a content type set, using this URL as the src of an img tag fails intermittently with error DOM7009.
Opera 15 Safari 6 WebView Android Yes Chrome Android 25 Firefox Android 19
19
createObjectURL() is no longer available within the context of a ServiceWorker .
Opera Android 14 Safari iOS 6 Samsung Internet Android 1.5
No longer accepts MediaStream 对象 Chrome ?
?
here for progress on deprecation.
Edge ?
?
here for progress on deprecation.
Firefox 62 IE ? Opera ?
?
here for progress on deprecation.
Safari ?
?
here for progress on deprecation.
WebView Android ?
?
here for progress on deprecation.
Chrome Android ?
?
here for progress on deprecation.
Firefox Android 62 Opera Android ?
?
here for progress on deprecation.
Safari iOS ?
?
here for progress on deprecation.
Samsung Internet Android ?
?
here for progress on deprecation.

图例

完整支持

完整支持

兼容性未知 ?

兼容性未知

实验。期望将来行为有所改变。

实验。期望将来行为有所改变。

见实现注意事项。

另请参阅

元数据

  • 最后修改:
  1. URL API
  2. URL
  3. 构造函数
    1. URL()
  4. 特性
    1. hash
    2. host
    3. hostname
    4. href
    5. origin
    6. password
    7. pathname
    8. port
    9. 协议
    10. search
    11. searchParams
    12. username
  5. 方法
    1. createObjectURL()
    2. revokeObjectURL()
    3. toJSON()
    4. toString()
  6. Related pages for URL API
    1. HTMLHyperlinkElementUtils
    2. URLSearchParams
    3. URLUtilsReadOnly

版权所有  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1