1

I have the byte array in javascript. How convert this array to file for upload?

4
  • Is this what you are after? stackoverflow.com/questions/12603307/… Commented Jan 9, 2013 at 9:12
  • For upload? Why would you want to convert anything? Just send it via AJAX and do whatever you want with it on the server side. Commented Jan 9, 2013 at 9:16
  • Some more details about what you want will help. Commented Jan 9, 2013 at 9:27
  • There is HTML5 solution stackoverflow.com/q/23451726/2587343 Commented Jun 18, 2017 at 18:11

2 Answers 2

1

you create a file like this

new File([you byte here], 'name of your file', { type: 'you extention', lastModified: new Date() });

if the your byte is string 

  new File([this.base64ToArrayBuffer(you string)], 'file name', { type: this.handleFileDataType(DocExtension), lastModified: new Date() });



 base64ToArrayBuffer = (base64) => {
    var binaryString = window.atob(base64);
    var binaryLen = binaryString.length;
    var bytes = new Uint8Array(binaryLen);
    for (var i = 0; i < binaryLen; i++) {
      var ascii = binaryString.charCodeAt(i);
      bytes[i] = ascii;
    }
    return bytes;
  }



 handleFileDataType = ext => {
    switch (ext) {
      case 'pdf':
        return 'application/pdf';
      case 'jpg':
        return 'image/jpeg';
      case 'jpeg':
        return 'image/jpeg';
      case 'png':
        return 'image/png';
      case 'tiff':
        return 'image/tiff';
      case 'docx':
        return 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
    }
  };
Sign up to request clarification or add additional context in comments.

Comments

0

What I got from your question is that you have a Byte Array and you want to write these Byte Array to a file and upload it to server.

No, It is not possible in JavaScript because JavaScript doesn't have access to writing files as this would be a huge security risk.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.