0

I am trying to convert base64 data to file using javascript on asp.net, but i am getting( 0x800a01bd - JavaScript runtime error: Object doesn't support this action) error on final stage while converting blob to file at final stage.

Here is my code:

function dataBaseURLtoFile(str) {
    // extract content type and base64 payload from original string
    var pos = str.indexOf(';base64,');
    var type = str.substring(5, pos);
    var b64 = str.substr(pos + 8);

    // decode base64
    var imageContent = atob(b64);

    // create an ArrayBuffer and a view (as unsigned 8-bit)
    var buffer = new ArrayBuffer(imageContent.length);
    var view = new Uint8Array(buffer);

    // fill the view, using the decoded base64
    for (var n = 0; n < imageContent.length; n++) {
        view[n] = imageContent.charCodeAt(n);
    }

    // convert ArrayBuffer to Blob
    var blob = new Blob([buffer], { type: type });

    //convert blob to file

    var file = new File([blob], "name", { type: "image/jpeg", });

    return file;
}
Sagar Timalsina
  • 191
  • 3
  • 14
  • Does this answer your question? [How to convert Base64 String to javascript file object like as from file input form?](https://stackoverflow.com/questions/35940290/how-to-convert-base64-string-to-javascript-file-object-like-as-from-file-input-f) – Tân Dec 03 '19 at 04:24
  • No, I am getting same error '0x800a01bd - JavaScript runtime error: Object doesn't support this action' also with this solution. – Sagar Timalsina Dec 03 '19 at 04:35
  • var block = strL.split(";"); // Get the content type of the image var type = block[0].split(":")[1];// In this case "image/gif" // you can al so assign // var type = 'base64'; var pos = str.replace('data:;base64,', ''); – LDS Dec 03 '19 at 04:58

1 Answers1

1

I try to check your code and found that issue is on line below.

 var file = new File([blob], "name", { type: "image/jpeg", });

IE and Edge browser does not supports the File() constructor.

File.File() constructor

For IE and Edge browser you need to use any alternative way.

You can try to refer thread below may give you some helpful information about alternative ways.

Is there an alternative for File() constructor for Safari and IE?

Deepak-MSFT
  • 10,379
  • 1
  • 12
  • 19