Convert Data URI to File then append to FormData

后端 未结 14 2636
逝去的感伤
逝去的感伤 2020-11-21 07:14

I\'ve been trying to re-implement an HTML5 image uploader like the one on the Mozilla Hacks site, but that works with WebKit browsers. Part of the task is to extract an imag

14条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-21 07:40

    BlobBuilder and ArrayBuffer are now deprecated, here is the top comment's code updated with Blob constructor:

    function dataURItoBlob(dataURI) {
        var binary = atob(dataURI.split(',')[1]);
        var array = [];
        for(var i = 0; i < binary.length; i++) {
            array.push(binary.charCodeAt(i));
        }
        return new Blob([new Uint8Array(array)], {type: 'image/jpeg'});
    }
    

提交回复
热议问题