简体   繁体   中英

Javascript - Callback

I am new to Javascript and am working on a task to compress and then upload an already uploaded image.

I am trying to:

  1. Retrieve the uploaded image,
  2. Compress it
  3. Convert it to a base64 URL
  4. Convert it into a blob
  5. And then into a file and upload it.

But this code just doesn't work.

When I step through it using a debugging tool it does it's job but otherwise it doesn't. I think the rest of the code after the loadImage function call doesn't really execute.

Please help me make sense of it! Thanks!

function loadImage(formObj2, fldid2, file, callback) {
  var oldImage = document.createElement("img");
  var psImageOutput = new Image();
  var reader = new FileReader();
  reader.onload = function(e) {
    /* code to compress image */
    callback(psImageOutput);
  }
  reader.readAsDataURL(file);
}


var inputFile = fileQueue[i].file;
var formObj1 = formObject;
var fldid1 = fldid;
loadImage(formObj1, fldid1, inputFile, function(psImageOutput) {
  var newImageDataSRC = psImageOutput.src;
  /* Manipulate SRC string and create a blob and an image file from it */
  formObj1.append(fldid1, newimgfile);
});

Be careful, on the line :

  formObj1.append(fldid1, newimgfile);

You seem to append a dom node called newimgfile but in your code this variable doesn't exist.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM