Search code examples
angularbase64ng2-file-upload

How can I send an image in base64 with ng2-file-upload?


The title is my question, how I can send an image in base64 with ng2-file-upload?

My code in Angular 4:

public uploader: FileUploader = new FileUploader({
   url: URL,
   allowedMimeType: ['application/pdf', 'image/jpeg', 'image/png'],
   maxFileSize: 10 * 1024 * 1024, // 10 MB
   queueLimit: 3,
   disableMultipart: true,
});

Solution

  • I solved this way:

    saveImages() {
        let fileCount: number = this.uploader.queue.length;
        if (fileCount > 0) {
        this.uploader.queue.forEach((val, i, array) => {
            let fileReader = new FileReader();
            fileReader.onloadend = (e) => {
                let imageData = fileReader.result;
                let rawData = imageData.split("base64,");
                if (rawData.length > 1) {
                    rawData = rawData[1];
                }
            }
            fileReader.readAsDataURL(val._file);
        });
    }
    

    took from here: https://github.com/valor-software/ng2-file-upload/issues/949