Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I was working on Ajax web application last time, and realized that we cannot upload a File using Ajax. I saw it on many sites, and tutorials saying we cannot upload a file using Ajax, for that we need to use Flash or iframe's etc..to do so. But why it is not possible? In future will that be possible?

share|improve this question

closed as primarily opinion-based by gnat, MichaelT, GlenH7, Ozz, Jimmy Hoffa Sep 27 '13 at 16:11

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise.If this question can be reworded to fit the rules in the help center, please edit the question.

"Flash of iframe's"?? :) –  Mayank Jan 19 '11 at 7:17
@Mayank oops! :D –  Harish Kurup Jan 19 '11 at 7:21
Move to StackOverflow? –  NickC Jan 19 '11 at 8:04
@Renesis actually i am not looking for the code..or help in coding for now just a discussion on why? –  Harish Kurup Jan 19 '11 at 8:07
add comment

3 Answers

up vote 6 down vote accepted

Uploading files is deliberately hard to avoid security leaks. If you could do that with a few lines of JavaScript, evil websites could secretly upload local files. For that reason, it must be done through a form, the user must see a file upload control, must choose the file by himself. After that, the form has to be submitted, and that's normal HTTP transfer, not Ajax, because XMLHttpRequest doesn't support multipart/form-data uploads. Therefore the iframes to gather the result. But those iframes can be invisible, and they can be processed after loading.

share|improve this answer
ok i got that, Ajax also does a HTTP transfer i think, and but my question was can we do file upload through forms using Ajax method. like we have a file upload element and send that file data using XMLHttpRequest object. –  Harish Kurup Jan 19 '11 at 7:53
just edited my answer regarding that question - XMLHttpRequest doesn't support multipart/form-data uploads, so you can't. What's the problem? The method using iframes works well, doesn't it? –  user281377 Jan 19 '11 at 7:55
just found this link, seems like FF can do a bit more - but that's not crossplattform, so think it's relatively useless by now: igstan.ro/posts/… –  user281377 Jan 19 '11 at 7:59
Actually, you could still do a file transfer via a PUT or POST (even manually encoding the multipart/form-data). The reason you can't do file uploads via Ajax is because browsers restrict access to the local filesystem: you can't get to the contents of the file the user selected via javascript, so you can't upload it. –  Dean Harding Jan 19 '11 at 8:01
Dean: true, thanks for the correction, though it's not a file transfer if there is no file to be transfered ;-) –  user281377 Jan 19 '11 at 8:11
show 1 more comment

Well you can do that right now using JQuery Form Plugin. Have a look at the demos here - http://jquery.malsup.com/form/

share|improve this answer
but the plugins might be using some iframes or flash..and not purely through Ajax..i think..cos we were using Uploadify. @Mayank but r u sure?? –  Harish Kurup Jan 19 '11 at 7:24
@Harish I am pretty sure that there is no flash involved as jQuery is pure javascript and also because I have used it. Have a look at my answer here - stackoverflow.com/questions/4625377/… –  Mayank Jan 19 '11 at 7:28
can u tell how to do it without jQuery i.e in core Javascript.(sorry not doubting u, i am confused need to get out..) –  Harish Kurup Jan 19 '11 at 7:32
@Harish I am not sure what you mean by "core javascript". "jQuery is a fast and concise JavaScript Library that simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development. jQuery is designed to change the way that you write JavaScript." –  Mayank Jan 19 '11 at 8:21
i was talking about the XMLHttpRequest object and not jQuery, the question was why we can't use Ajax to upload a file! –  Harish Kurup Jan 19 '11 at 8:32
add comment

Upload support is part of the XMLHttpRequest Level 2 Working Draft. It's currently supported by Safari and Chrome, and should be supported later in the year by new versions of Firefox and Internet Explorer.

XHR2 Browser Support Table

share|improve this answer
thanks for the Updates.. –  Harish Kurup Jan 19 '11 at 9:12
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.