Convert base64 image data to image file in angularjs

后端 未结 3 1089
执念已碎
执念已碎 2021-01-18 08:50

getting corrupted file while converting base64 file to image in angularjs can anyone suggest me how to convert base64 file to image in angularjs

I am using this meth

3条回答
  •  时光说笑
    2021-01-18 09:52

    First, you convert dataURL to Blob do this

    var blob = dataURItoBlob(imageBase64);
    
    function dataURItoBlob(dataURI) {
    
                // convert base64/URLEncoded data component to raw binary data held in a string
                var byteString;
                if (dataURI.split(',')[0].indexOf('base64') >= 0)
                    byteString = atob(dataURI.split(',')[1]);
                else
                    byteString = unescape(dataURI.split(',')[1]);
    
                // separate out the mime component
                var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
    
                // write the bytes of the string to a typed array
                var ia = new Uint8Array(byteString.length);
                for (var i = 0; i < byteString.length; i++) {
                    ia[i] = byteString.charCodeAt(i);
                }
    
                return new Blob([ia], {type:mimeString});
            }
    

    then

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

提交回复
热议问题