251

Display Below error in Safari.

Failed to execute 'createObjectURL' on 'URL': No function was found that matched the signature provided.

My Code is:

function createObjectURL(object) {
    return (window.URL) ? window.URL.createObjectURL(object) : window.webkitURL.createObjectURL(object);
}

This is my Code for image:

function myUploadOnChangeFunction() { 
    if (this.files.length) { 
       for (var i in this.files) { 
           if (this.files.hasOwnProperty(i)) { 
              var src = createObjectURL(this.files[i]); 
              var image = new Image(); 
              image.src = src; 
              imagSRC = src; 
              $('#img').attr('src', src); 
            }
       }           
   } 
} 
georgeawg
  • 48,608
  • 13
  • 72
  • 95
Hardik Mandankaa
  • 3,129
  • 4
  • 23
  • 32
  • 7
    Hi Hardik, what are you passing to your `createObjectURL(...)` function when you get that error? – Arthur Weborg Nov 25 '14 at 07:38
  • 4
    object must be a File object or a Blob object to create a object URL for.see http://devdocs.io/dom/window.url.createobjecturl – yxf Nov 25 '14 at 07:38
  • 1
    This is my Code for image: function myUploadOnChangeFunction() { if (this.files.length) { for (var i in this.files) { if (this.files.hasOwnProperty(i)) { var src = createObjectURL(this.files[i]); var image = new Image(); image.src = src; imagSRC = src; $('#img').attr('src', src); } } } } – Hardik Mandankaa Nov 25 '14 at 07:46
  • @HardikMansaraa Go ahead and edit that in to your question. – soktinpk Dec 05 '14 at 03:16
  • `window.URL.createObjectURL('broken')` throws an error: `Uncaught TypeError: Failed to execute 'createObjectURL' on 'URL': No function was found that matched the signature provided.` – Ruan Mendes Nov 17 '15 at 14:36
  • I had the same problem because I was importing `JSZip.js` twice. – Washington Guedes Feb 20 '17 at 14:21
  • @yxf is right, I was getting this error because I was passing an `ArrayBuffer` instead of a `Blob` – andrewdotn Sep 02 '20 at 23:21
  • using Vue.js, when I change `fetch(...).then((response) => response.blob()).then(function(blob){var url = window.URL.createObjectURL(blob)})` to `fetch(...).then((response) => { response.blob()} ).then(function(blob){var url = window.URL.createObjectURL(blob)})`, this error occurs. it should be `then((response) => { return response.blob()} )` or just `then((response) => response.blob() )`. – Brent81 Sep 15 '21 at 06:09

12 Answers12

286

I experienced the same error, when I passed raw data to createObjectURL:

window.URL.createObjectURL(data)

It has to be a Blob, File or MediaSource object, not data itself. This worked for me:

var binaryData = [];
binaryData.push(data);
window.URL.createObjectURL(new Blob(binaryData, {type: "application/zip"}))

Check also the MDN for more info: https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL


UPDATE

Back in the day we could also use createObjectURL() method with MediaStream objects. This use has been dropped by the specs and by browsers.
If you need to set a MediaStream as the source of an HTMLMediaElement just attach the MediaStream object directly to the srcObject property of the HTMLMediaElement e.g. <video> element.

const mediaStream = new MediaStream();
const video = document.getElementById('video-player');
video.srcObject = mediaStream;

However, if you need to work with MediaSource, Blob or File, you still have to create a blob:// URL with URL.createObjectURL() and assign it to HTMLMediaElement.src.

Read more details here: https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/srcObject

Kaiido
  • 123,334
  • 13
  • 219
  • 285
mimo
  • 6,221
  • 7
  • 42
  • 50
  • 2
    Hi.. What to do if I am dealing with "application/pdf" ? I am getting the same error on console when I am dealing with PDF file – N Sharma Oct 29 '18 at 04:45
  • @mimo I am using same code to download file. But two files are getting downloaded. One is correct file and one extra file with same name but failed status is getting downloaded. Do you have any idea why it is happening? – Shardul Mar 13 '19 at 06:26
  • I'm confused with this comment, in MDN it discourages the use of `URL.createObjectURL()` for media streams. However it doesn't state NOT to use it for a file input as stated in the initial question. – alextrastero May 25 '20 at 13:57
186

This error is caused because the function createObjectURL no longer accepts media stream object for Google Chrome

I changed this:

video.src=vendorUrl.createObjectURL(stream);
video.play();

to this:

video.srcObject=stream;
video.play();

This worked for me.

Christian
  • 2,094
  • 1
  • 12
  • 12
  • +1 See example with fallback https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/srcObject – eddyparkinson Feb 15 '19 at 05:12
  • 10
    createObjectURL is not deprecated as shown [here](https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL#Browser_compatibility) but no longer accepts media stream object – dwp4ge Jul 27 '19 at 12:21
  • This should be the answer. – DomingoR Oct 16 '19 at 10:00
  • there is one another problem video.play() seems to be restricted : DOMException: play() can only be initiated by a user gesture. – user889030 Mar 24 '20 at 11:46
  • @user889030 that just means you can't open a webpage and expect the webcam stream to start. You have to let the user explicitly start the stream. Just use a button to start the stream – SRR Jun 10 '20 at 00:31
  • Sorry to downvote, but while this answer is correct, it does not answer the OP's question which was about files, not media streams. – Marcel Dec 03 '21 at 06:18
36

My code was broken because I was using a deprecated technique. It used to be this:

video.src = window.URL.createObjectURL(localMediaStream);
video.play();

Then I replaced that with this:

video.srcObject = localMediaStream;
video.play();

That worked beautifully.

EDIT: Recently localMediaStream has been deprecated and replaced with MediaStream. The latest code looks like this:

video.srcObject = new MediaStream();

References:

  1. Deprecated technique: https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL
  2. Modern deprecated technique: https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/srcObject
  3. Modern technique: https://developer.mozilla.org/en-US/docs/Web/API/MediaStream
Dani Amsalem
  • 1,266
  • 17
  • 26
10

Video with fall back:

try {
  video.srcObject = mediaSource;
} catch (error) {
  video.src = URL.createObjectURL(mediaSource);
}
video.play();

From: https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/srcObject

eddyparkinson
  • 3,680
  • 4
  • 26
  • 52
9

I had the same error for the MediaStream. The solution is set a stream to the srcObject.

From the docs:

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.

Ezra Steinmetz
  • 1,267
  • 1
  • 17
  • 18
max.kovpak
  • 91
  • 1
  • 2
5

The problem is that the keys provided in the loop do not refer to the index of the file.

for (var i in this.files) {
    console.log(i);
}

The output of the above code is:

0
length
item

But what was expected was:

0
1
2
etc...

Then the error occurs when the browser tries to execute, for example:

window.URL.createObjectURL(this.files["length"])

I suggest implementation based on the following code:

var files = this.files;
for (var i = 0; i < files.length; i++) {
    var file = files[i],
        src = (window.URL || window.webkitURL).createObjectURL(file);
    ...
}

I hope this can help someone.

Greetings!

Hiago Takaki
  • 188
  • 2
  • 7
2

If you are using ajax, it is possible to add the options xhrFields: { responseType: 'blob' }:

$.ajax({
  url: 'yourURL',
  type: 'POST',
  data: yourData,
  xhrFields: { responseType: 'blob' },
  success: function (data, textStatus, jqXHR) {
    let src = window.URL.createObjectURL(data);
  }
});
Guilherme Porto
  • 101
  • 1
  • 4
2

If you are using angular this tutorial will be helpful: link. However you will need to replace this line:

this.video.src = window.URL.createObjectURL(stream);

with this, since createObjectURL() is deprecated on chrome for MediaStream.

this.video.srcObject = stream;
tech_ank
  • 59
  • 3
2
//my code was:

this._videoEl = videoEl;
        navigator.mediaDevices.getUserMedia({
            video : true
        }).then(stream => {
            this._videoEl.src = URL.createObjectURL(stream);
            this._videoEl.play();
        }).catch(err => {
            console.log(err);
        });

//and replace to this worked for me :

this._videoEl = videoEl;
        navigator.mediaDevices.getUserMedia({
            video : true
        }).then(stream => {
            this._videoEl.srcObject = stream;
            this._videoEl.play();
        }).catch(err => {
            console.log(err);
        });
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 23 '22 at 04:53
  • This answer aligns with more recent MDN examples at https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia which show `video.srcObject = mediaStream;` without using `URL.createObjectURL()`. – OXiGEN Aug 17 '23 at 06:43
0

I tried few things, but for me simply assigning stream to src worked.

video.srcObject=stream;
KushalSeth
  • 3,265
  • 1
  • 26
  • 29
0

I was able to fix this by checking if the object is null. {object ? URL.createObjectURL(object) : "default.png"}

This made me to conclude that the error occur when object is null.

Jokanola
  • 66
  • 5
-10

I fixed it bydownloading the latest version from GitHub

schoon
  • 2,858
  • 3
  • 46
  • 78
AutoCiudad
  • 735
  • 1
  • 10
  • 14