0

Does someone know how to get <input type="file"> value to pass through an Ajax call?

Instead of:

// more...

function uploadFile(event) {
        var file = event.target.files;
        event.stopPropagation();
        event.preventDefault();

        var data = new FormData();

        $.each(
            file,
            function(key, value) {
                data.append(key, value);
            }
        );

        $.ajax({
            url: 'site/upload',
            type: 'POST',
            data: data,
// more...

The uploadFile() is called on change of the input file. The problem with using FormData object is that it's not supported in IE 10-.

2
  • you can simply send() the form or file, but a lot of folks don't know that... Commented Feb 18, 2014 at 18:48
  • i thought IE9 supports ajax2... Commented Feb 18, 2014 at 18:54

1 Answer 1

5

How to get input file value instead of using FormData

You can't.

Without FormData, your only options are Flash and posting to an iframe.

I suggest using FormData if it is currently available, else just submit it to an iframe or cause a full page postback.

Or, you can use one of the many plugins that already do this for you.

Sign up to request clarification or add additional context in comments.

4 Comments

what about FileReader(), does that not count? it should: caniuse.com/filereader
It also isn't supported IE<10, so that doesn't help. developer.mozilla.org/en-US/docs/Web/API/FileReader
Also, FileReader is not used to upload files, only to read them client-side, which is not necessary if you simply intend to upload a selected file.
well, technically, if you could read the file, you could send it's data:uri to the server as text, so it could help, if it were even available(but it's not)

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.