javascript - How to load a PDF into a blob so it can be uploaded? -


i'm working on testing framework needs pass files drop listener of plupload instance. need create blob objects pass inside data transfer object of sort generated on drag / drop event. have working fine text files , image files. add support pdf's, seems can't encoding right after retrieving response. response coming text because i'm using sahi retrieve in order avoid cross-domain issues.

in short: string i'm receiving utf-8 encoded , therefore content looks opened pdf text editor. wondering how convert necessary format create blob, after document gets uploaded looks okay.

what steps need go through convert utf-8 string proper blob object? (yes, aware submit xhr request , change responsetype property , (maybe) closer, due complications way sahi operates i'm not going explain here why prefer not go route).

also, i'm not familiar enough have hunch maybe lose data retrieving string? if that's case i'll find approach.

the existing code , recent approach have tried here:

    var data = '%pdf-1.7%����115 0 obj<</linearized 1/l ...'     var arr = [];     var utf8 = unescape(encodeuricomponent(data));     (var = 0; < utf8.length; i++) {         arr.push(utf8.charcodeat(i));     }      var file = new blob(arr, {type: 'application/pdf'}); 

it looks close. did site needed read pdf website , drop fileuploader plugin. here worked me:

    var url = "http://some-websites.com/pdf/";      //you may not need part if have pdf data locally     var xhr = new xmlhttprequest();     xhr.onreadystatechange = function () {         if (this.readystate == 4 && this.status == 200) {             //console.log(this.response, typeof this.response);             //now convert blob response file , give name             var fileofblob = new file([this.response], 'your_file.pdf');              // file             // filuploader (blueimp), use add method             $('#fileupload').fileupload('add', {                 files: [ fileofblob ],                  fileinput: $(this)             });         }     }     xhr.open('get', url);     xhr.responsetype = 'blob';     xhr.send();   

i found on xhr blob here. this answer helped me naming file. might able use blob itself, won't able give name unless passed file.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -