Convert a file into a binary array from JavaScript

0

Hello, I would like to know if there is a way to convert a file / file into a binary object from js and then pass it as a parameter in ajax . If we assume that I am taking a file from a input type = file and then I want to pass it to the server using C #. An Example would be incredible. Thanks

    
asked by E.Rawrdríguez.Ophanim 16.11.2017 в 01:52
source

1 answer

1

recently I did that and I pass my portion of code, hopefully it will help:

$('#myFile').on('change', function (e) {

                 readFile(this.files[0], function (e) {
                      bytes = (e.target.result);
                      console.log(bytes);
                 });

});

This will print something like this: data: application / pdf; base64, JVBERi0xLjUKJYCAgIAKMTEgMC ...

However, if you are going to occupy it in ajax, the data: application / pdf; base64 part will not be very useful, you can remove it with a:

bytes.replace("data:" + formato + ";base64,", "")

the format you take with a:

formato = this.files[0].type;

It is worth mentioning that this occupies it to attach a file and be able to send it through an email which was a WS in JAVA

    
answered by 16.11.2017 / 02:13
source