0

How to convert base64 string into file object in Javascript which should work in IE Browser

function dataURLtoFile(dataurl: any, filename: string) {
        var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
            bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
        while (n--) {
            u8arr[n] = bstr.charCodeAt(n);
        }
        return new File([u8arr], filename, { type: mime });
}

This is not working in IE browser edge.

1

2 Answers 2

2

After reading lots of tutorials I find a conclusion that Blob is also treated as File and we can post Blob into formdata.

var base64data = window.localStorage.getItem("img").replace("data:image/png;base64,", "");
        var bs = atob(base64data);
        var buffer = new ArrayBuffer(bs.length);
        var ba = new Uint8Array(buffer);
        for (var i = 0; i < bs.length; i++) {
            ba[i] = bs.charCodeAt(i);
        }
        var file = new Blob([ba], { type: "image/png" });
Sign up to request clarification or add additional context in comments.

1 Comment

atob is deprecated now, here is what it says: @deprecated — Use Buffer.from(data, 'base64') instead.
0

Add this:

<script src="https://cdn.polyfill.io/v2/polyfill.min.js"></script>

This adds js compability into older browsers

2 Comments

Thank you jan ...But I am using Angular2. So is there any other way to do the same.
this didnt help?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.