Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to access image data #949

Closed
jojoblaze opened this issue Dec 21, 2017 · 5 comments
Closed

how to access image data #949

jojoblaze opened this issue Dec 21, 2017 · 5 comments

Comments

@jojoblaze
Copy link

Hi, I need to send a custom request to a webservice.
I need to access image data in order to convert it in Base64 and send these data with others.
Someone can help, please?

@sime2408
Copy link

sime2408 commented Dec 22, 2017

Try this:

constructor() {
    this.uploaderImage = new FileUploader({});
    // extract binary
    const extract = function (dataURI) {
      const binary = atob(dataURI.split(',')[1]);
      const array = [];
      for (let i = 0; i < binary.length; i++) {
        array.push(binary.charCodeAt(i));
      }
      return array;
    };

   // event  --- uncomment what you want
    this.uploaderImage.onAfterAddingFile = function (item) {
      const reader = new FileReader();
      reader.onload =  (event) => {
        //console.log(extract(reader.result));
        //console.log(reader.result);
      };
      reader.readAsDataURL(item._file);
      //reader.readAsText(item._file);
    }
  }

@jojoblaze
Copy link
Author

jojoblaze commented Jan 9, 2018

Thank you very much, 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];

        // create my model here

        // call my service here
      }
    }
    fileReader.readAsDataURL(val._file);

  });
}

`

@DzmVasileusky
Copy link

DzmVasileusky commented May 24, 2018

Hi.
I'm getting Typescript error "argument type File is not assignable to parameter type Blob" when using ._file as fileReader.readAsDataURL parameter.
MDN says it can be File or Blob
https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsDataURL

Any thoughts?

@DzmVasileusky
Copy link

It looks like a bug in Webstorm JavaScriptLanguage plugin.
It defines readAsDataURL(blob: Blob): void;

@mvgadagi
Copy link

I am facing issue in converting to Base64 can you please help me, Below is my code can you help me

public uploader: FileUploader = new FileUploader({
    url: 'http://test.com',
    itemAlias: 'photo',
    authToken: 'Bearer ' + this.accessToken,
    headers: [{ name: 'Content-Type', value: 'application/json; charset=utf-8' }],
    disableMultipart: true,
    formatDataFunctionIsAsync: true,
    formatDataFunction: async (item) => {
        return new Promise((resolve, reject) => {
            resolve({
                documentType: 'Photo',
                contentType: 'PNG',
                documentCode: '1234',
                uploadCode: '564',
                data: this.extractBinary(this.uploader.queue[0])
            });
        });
    }
});

extractBinary(file) {
    file.withCredentials = false;
    const reader = new FileReader();
    let binaryValue;
    reader.onload = (event) => {
        binaryValue = reader.result;
        return binaryValue;
    };
    return reader.readAsDataURL(file._file);
}


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants