URL.createObjectURL()

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, or MediaSource 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.

Warning: If you still have code that relies on createObjectURL() to attach streams to media elements, you need to update your code to set srcObject to the MediaStream directly.

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
createObjectURL
19
12
19
createObjectURL() is no longer available within the context of a ServiceWorker.
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.
15
6
≤37
25
19
createObjectURL() is no longer available within the context of a ServiceWorker.
14
6
1.5
MediaStream_support
19-71
12-79
19-62
10
15-60
6-11
≤37-71
25-71
19-62
14-50
6-11
1.5-10.0

See also

© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL