javascript - jsZip opened png image, POST it into server with ajax -
trying post .png image files zip jszip. same code works when trying same stuff .xml files , .mod files, not working .png files.
the code i'm using is:
jszip.loadasync(f) // f .zip file in input field .then(function(zip) { zip.foreach(function (relativepath, zipentry) { zipentry.async("string").then(function (data) { //data png image var pngfilepath="/serverimagespath/" + zipentry.name; var blob = dataurltoblob(data); $.ajax({ type: "post", url: pngfilepath, data: blob, datatype: "binary", }).done(function ( ) { console.log('put correctly png- ' + pngfilepath); }).fail(function ( jqxhr, textstatus, errorthrown ) { console.log("err png: " + errorthrown, textstatus); }); }); }); }); function dataurltoblob(dataurl) { var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1], bstr = atob(arr[1]), n = bstr.length, u8arr = new uint8array(n); while(n--){ u8arr[n] = bstr.charcodeat(n); } return new blob([u8arr], {type:mime}); }
what i'm doing wrong?
$.ajax
try process content unicode string. add processdata: false
, see this answer (and jquery documentation):
$.ajax({ type: "post", url: pngfilepath, data: blob, processdata: false })
you can simplify bit code, .async()
supports blob:
zipentry.async("blob").then(function (blob) { // ... $.ajax({ type: "post", url: pngfilepath, data: blob, contenttype: "image/png", // or compute processdata: false }) // ...
Comments
Post a Comment