Get data from file input in JQuery

kschaeffler picture kschaeffler · Sep 5, 2012 · Viewed 399.5k times · Source

I actually have a file input and I would like to retrieve the Base64 data of the file.

I tried:

$('input#myInput')[0].files[0] 

to retrieve the data. But it only provides the name, the length, the content type but not the data itself.

I actually need these data to send them to Amazon S3

I already test the API and when I send the data through html form with encode type "multipart/form-data" it works.

I use this plugin : http://jasny.github.com/bootstrap/javascript.html#fileupload

And this plugins gives me a preview of the picture and I retrieve data in the src attribute of the image preview. But when I send these data to S3 it does not work. I maybe need to encode the data like "multipart/form-data" but I don't know why.

Is there a way to retrieve these data without using an html form?

Answer

Morilog picture Morilog · Oct 30, 2013

input file element:

<input type="file" id="fileinput" />

get file :

var myFile = $('#fileinput').prop('files');