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 available in Web Workers.
Note: This feature is not available in Service Workers due to its potential to create memory leaks.
Syntax
const objectURL = URL.createObjectURL(object)
Parameters
object
- A
File
,Blob
, orMediaSource
object to create an object URL for.
Return value
A DOMString
containing an object URL that can be used to reference the contents of the specified source object
.
Examples
See Using object URLs to display images.
Usage notes
Memory management
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.
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.
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.
Specifications
Specification | Status | Comment |
File APIThe definition of 'createObjectURL()' in that specification. | Working Draft | Initial definition. |
Media Source ExtensionsThe definition of 'URL' in that specification. | Recommendation |
MediaSource extension. Older versions of this specification used |
Browser compatibility
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.
Update compatibility data on GitHub
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Chrome
Full support 19 |
Edge
Full support 12 |
Firefox Full support 19 Full support 19 Notes' |
IE Full support 10 Full support 10 Notes' If the underlying object does not have a content type set, using this URL as the |
Opera
Full support 15 |
Safari
Full support 6 |
WebView Android
Full support Yes |
Chrome Android
Full support 25 |
Firefox Android Full support 19 Full support 19 Notes' |
Opera Android
Full support 14 |
Safari iOS
Full support 6 |
Samsung Internet Android
Full support 1.5 |
No longer accepts MediaStream object
|
Chrome ? ? Notes' See here for progress on deprecation. |
Edge ? ? Notes' See here for progress on deprecation. |
Firefox
Full support 62 |
IE
? |
Opera ? ? Notes' See here for progress on deprecation. |
Safari ? ? Notes' See here for progress on deprecation. |
WebView Android ? ? Notes' See here for progress on deprecation. |
Chrome Android ? ? Notes' See here for progress on deprecation. |
Firefox Android
Full support 62 |
Opera Android ? ? Notes' See here for progress on deprecation. |
Safari iOS ? ? Notes' See here for progress on deprecation. |
Samsung Internet Android ? ? Notes' See here for progress on deprecation. |
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.
See also
- Using files from web applications
- Using object URLs to display images
URL.revokeObjectURL()
HTMLMediaElement.srcObject
FileReader.readAsDataURL()
URL.createObjectURL() by Mozilla Contributors is licensed under CC-BY-SA 2.5.