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()
.
注意: 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.
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
|
Chrome 19 | Edge 12 |
Firefox
19
|
IE
10
|
Opera 15 | Safari 6 | WebView Android Yes | Chrome Android 25 |
Firefox Android
19
|
Opera Android 14 | Safari iOS 6 | Samsung Internet Android 1.5 |
No longer accepts
MediaStream
对象
|
Chrome
?
|
Edge
?
|
Firefox 62 | IE ? |
Opera
?
|
Safari
?
|
WebView Android
?
|
Chrome Android
?
|
Firefox Android 62 |
Opera Android
?
|
Safari iOS
?
|
Samsung Internet Android
?
|
完整支持
兼容性未知
实验。期望将来行为有所改变。
见实现注意事项。
URL.revokeObjectURL()
HTMLMediaElement.srcObject
FileReader.readAsDataURL()
URL
createObjectURL()
revokeObjectURL()
toJSON()
toString()