1

I have this PDF file which i need to convert into an array of byte value numbers.

I have previously converted it to base64 but the conversion of PDF to base64 took forever for a 2MB PDF. Is there another way to convert PDF to base64 faster OR convert that PDF file into array of bytes(to see if its faster than converting to base64)?

i am using filereader to read the file into dataURL previously. Below is the code i am using to convert to base64.

getFileContentAsBase64(path, callback) {
 window.resolveLocalFileSystemURL(path, gotFile, fail);

 function fail(e) {
   alert('Cannot found requested file');
 }

 function gotFile(fileEntry) {
   fileEntry.file(function (file) {
     var reader = new FileReader();
     reader.onloadend = function (e) {
       var content = this.result;
       callback(content);
     }
     reader.readAsDataURL(file);
   });
 }
}

EDIT:

getFileContentAsBase64(path, callback) {
 window.resolveLocalFileSystemURL(path, gotFile, fail);

 function fail(e) {
   alert('Cannot found requested file');
 }

 function gotFile(fileEntry) {
   fileEntry.file(function (file) {
     var reader = new FileReader();
     reader.onloadend = function (e) {
       var content = this.result;
       var array = new Uint8Array(content);
       var binaryString = String.fromCharCode.apply(null, array);
       callback(content);
     }
     reader.readAsArrayBuffer(file);
   });
 }

}
Oleg Estekhin
  • 8,063
  • 5
  • 49
  • 52
Lyon
  • 272
  • 1
  • 12
  • http://stackoverflow.com/a/32556944/1848109 i guess soln is already answered. – NiRUS May 09 '17 at 07:10
  • @Nirus i have seen that solution, however it does not seem to work in my case. i have edited my question above to show you what i have tried. Maybe my logic behind conversion is wrong... – Lyon May 09 '17 at 07:29
  • @Lyon: is this sequence maintained while loading scripts ` – Parth Ghiya May 09 '17 at 07:34
  • @Lyon what are you trying to achieve after converting into base64 or arraybuffer? and whats not working in your question(Edit part)? – NiRUS May 09 '17 at 07:37
  • @ParthGhiya i will change the sequence and try again. Give me a moment. – Lyon May 09 '17 at 07:52
  • @Nirus i am using Parse.File() to save objects into s3storage. However to save the file, i have 2 options. 1st: pass in array of bytes, 2nd: pass in base64 of the file. I tried using the base64 option, it is working however the conversion of file-base64 takes very long in handling bigger files(above 2MB). So im trying to change my codes to use the 1st option. – Lyon May 09 '17 at 07:52
  • @Lyon in this SO question OP has successfully used multipart form to upload the file http://stackoverflow.com/questions/28986701/creating-a-parse-file-with-javascript-sdk using `Parse.File` API. is this helpful? – NiRUS May 09 '17 at 08:03
  • @ParthGhiya i tried it, still doesnt work. but i have a feeling im passing in the wrong data. in the parse file api, they said an array of byte value numbers. But im converting using reader.readAsArrayBuffer... – Lyon May 09 '17 at 08:28
  • @Nirus http://www.uncorkedstudios.com/blog/multipartformdata-file-upload-with-angularjs/ this answer in their SO question is not what im trying to achieve though... thanks for you help. – Lyon May 09 '17 at 08:31

1 Answers1

0

i overcome the problem by converting the file into binaryString, which i then call a cloud function from my server side to do the conversion and saving of file. Thanks everyone for helping!~

Lyon
  • 272
  • 1
  • 12