Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert base64 image data to image file in angularjs

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 method to convert base64 file to image

var imageBase64 = "image base64 data";
var blob = new Blob([imageBase64], {type: 'image/png'});

From this blob, you can generate file object.

var file = new File([blob], 'imageFileName.png');
like image 863
Sagar Patel Avatar asked Nov 26 '15 13:11

Sagar Patel


People also ask

How to convert Base64 to image in angularjs?

check out this link. function getBase64Image(base64string) { return base64string. replace(/^data:image\/(png|jpg);base64,/, ""); } var imgData = JSON. stringify(getBase64Image(/* base64string */)); $.

How do I convert an image to Base64?

Using following code to convert it into an image file: function base64_to_jpeg( $base64_string, $output_file ) { $ifp = fopen( $output_file, "wb" ); fwrite( $ifp, base64_decode( $base64_string) ); fclose( $ifp ); return( $output_file ); } $image = base64_to_jpeg( $my_base64_string, 'tmp. jpg' );

What is Base64 in angular?

Base64 Encoding means of encoding text in ASCII(American Standard Code for Information Interchange) format. The Base64 scheme takes any kind of data type, such as binary data, and translates it into text format in ASCII. If you want to Encode, the method name is btoa().


2 Answers

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'"
          });
like image 98
byteC0de Avatar answered Sep 27 '22 00:09

byteC0de


Your code looks ok except for a point:

The data you are giving to the Blob object is not blob data, it's a text one this is base64 encoded. You should decode data before insert.

Once I don't know which API you would like, I will use a pseudofunction called decodeBase64 which we will understand do the inverse of the Base64 encode (there are many implementations for this function in web).

Your code should look like this:

// base64 already encoded data
var imageBase64 = "image base64 data";

//this is the point you should use
decodedImage = decodeBase64(imageBase64)

//now, use the decodedData instead of the base64 one
var blob = new Blob([decodedImage], {type: 'image/png'});

///now it should work properly
var file = new File([blob], 'imageFileName.png');

Anyway, I can't see the need to use AngularJS there once you are not already using.

like image 27
André Claudino Avatar answered Sep 25 '22 00:09

André Claudino