The 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
object.
To release an object URL, call revokeObjectURL()
.
Note: This feature is not available in Service Workers due to its potential to create memory leaks.
objectURL = URL.createObjectURL(object);
object
File
, Blob
or 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 object
.
See Using object URLs to display images.
Each time you call 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.
Important: 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
to the MediaStream
directly.
Specification | Status | Comment |
---|---|---|
File API The definition of 'createObjectURL()' in that specification. | Working Draft | Initial definition. |
Media Source Extensions The definition of 'URL' in that specification. | Recommendation | MediaSource extension. Older versions of this specification used |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | 8 | 12 | 4
|
10 | 15 | 6 |
No longer accepts MediaStream object |
?
|
? | 62 | ? | ?
|
?
|
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | ? | 18 | ? | 4
|
15 | 6 | ? |
No longer accepts MediaStream object |
? | ?
|
? | 62 | ?
|
?
|
? |
URL.revokeObjectURL()
HTMLMediaElement.srcObject
FileReader.readAsDataURL()
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL