File uploading using AJAX is not possible. AJAX doesn’t actually post forms to the server, it sends selected data to the server in the form of a POST or GET request. As javascript is not capable of grabbing the file from the users machine and sending it to the server, it’s just not possible with AJAX. You have to resort to regular old form submit. If you have read/seen it somewhere, then it is not through AJAX. File uploading occurs through an iframe in this case. You have to use a iframe to upload the files. So, you can use iframe to asynchronous upload (Like AJAX , but its not AJAX). How file uploading is done in Gmail? Use following JavaScript function, if you want to achieve same functionality.
<html>
<script language="Javascript">
function fileUpload(form, action_url, div_id) {
// Create the iframe...
var iframe = document.createElement("iframe");
iframe.setAttribute("id", "upload_iframe");
iframe.setAttribute("name", "upload_iframe");
iframe.setAttribute("width", "0");
iframe.setAttribute("height", "0");
iframe.setAttribute("border", "0");
iframe.setAttribute("style", "width: 0; height: 0; border: none;");
// Add to document...
form.parentNode.appendChild(iframe);
window.frames['upload_iframe'].name = "upload_iframe";
iframeId = document.getElementById("upload_iframe");
// Add event...
var eventHandler = function () {
if (iframeId.detachEvent) iframeId.detachEvent("onload", eventHandler);
else iframeId.removeEventListener("load", eventHandler, false);
// Message from server...
if (iframeId.contentDocument) {
content = iframeId.contentDocument.body.innerHTML;
} else if (iframeId.contentWindow) {
content = iframeId.contentWindow.document.body.innerHTML;
} else if (iframeId.document) {
content = iframeId.document.body.innerHTML;
}
document.getElementById(div_id).innerHTML = content;
// Del the iframe...
setTimeout('iframeId.parentNode.removeChild(iframeId)', 250);
}
if (iframeId.addEventListener) iframeId.addEventListener("load", eventHandler, true);
if (iframeId.attachEvent) iframeId.attachEvent("onload", eventHandler);
// Set properties of form...
form.setAttribute("target", "upload_iframe");
form.setAttribute("action", action_url);
form.setAttribute("method", "post");
form.setAttribute("enctype", "multipart/form-data");
form.setAttribute("encoding", "multipart/form-data");
// Submit the form...
form.submit();
document.getElementById(div_id).innerHTML = "Uploading...";
}
</script>
<!-- index.php could be any script server-side for receive uploads. -->
<form>
<input type="file" name="datafile" /></br>
<input type="button" value="upload"
onClick="fileUpload(this.form,'index.php','upload'); return false;" >
<div id="upload"></div>
</form>
</html>
Code language: JavaScript (javascript)
Hi! Thanks for such a nice tip, although data seem posted but one problem is that IFrame start downlaoding in IE what could be the issue is ?
Thanks for sharing! I used your code as a base and mixed in some jquery methods for the binding and it worked perfectly on ie and firefox.
You should add the var keyword to lines 31,33,35 before the content variable as IE7 throws an error. Very useful script though.
George
hm, can you show an example of a server-side script for receiving the upload.
This is awesome. Works great. Thank you!
I am unable to implement the server side script. In the server side script i gave my action class name but it throws an error. Can you please give me some example of how to implement the server side scripting
Very nice and easy to understand code. Thanks for sharing.
What should I do if I want to know when the uploading is done (e.g. to display new content to users) or to display a file-size-over-limit message to users?
Many thanks,
Fantab
Very interesting script. I am going to try it right now and will let you know how it goes.
Hats off! It totally worked. Thanks!
Pls place some demo works..
Thanks Just i want know execute this program in JSP
It’s been 1 hour since I read this and here I am gloating to my colleagues that i did what no one could do even after a weeks effort .. Awesome article .. Cheers!!
Please Anybody can tell me how to receive image at server side with example .
Hey if you want to use jQuery, I ported (not tested):
function asyncFormSubmit(form, resultDiv) {
// Create the iframe…
var iframe = $(”);
$(‘body’).append(iframe);
iframe.attr(‘name’, ‘upload_iframe’);
iframe.attr(‘id’, ‘upload_iframe’);
iframe.hide();
// When iframe loads, copy content back to target
function copyIframe () {
var content = $(iframe).contents().find(‘body’).html();
$(resultDiv).html(content);
setTimeout(function () { $(iframe).remove(); }, 250);
}
$(iframe).one(‘load’, copyIframe);
// Set properties of form…
$(form).attr(“target”,”upload_iframe”);
// Submit the form. This triggers the iframe to “load” the
// response from the server. Once loaded we can then do other
// stuff.
$(form).submit();
// This gets replaced by iframe content when ready
$(resultDiv).html(“Uploading…”);
}
I design a more simplified ajax style file upload page without using JavaScript. I place a hidden iFrame just before the form tag. Please read the following.
http://ramui.com/articles/ajax-file-upload-using-iframe.html
Thanks for sharing… It totally worked… Thanks..
thanks for the nice solution
just i want to know if can change the content-type for the uploaded file and if i can set or change request headers thanks.
Thanks….
Thanks Deepak :)
Thanks. Nice script. Worked perfectly on my first try.. Also, thanks foo bar for the jQuery snippet.. cheers!
window.frames[‘upload_iframe’].name = “upload_iframe”;
The above line when excluded in FF,it works excellent.
But when excluded in IE,instead of the file sitting in the iframe,a separate window opens.
What is the purpose of the above line? ( because we have already set the ‘name’ attribute for iframe in setAttribute() method ) ( to confirm if the name attribute was set,I printed in alert(iframe.name) and that worked fine too )
If setAtrribute is a problem in IE,can you please provide the reason for it and the solution for ir?
nice post
thanks….
Thanks. Nice script..
But I have some issue what am not able to upload more then 20KB.
Please. any one help me ..?
Maximum allowed sizes for file uploads are generally in the server settings. If you are using PHP for server side scripting, change the max allowed size in your .ini file. If you’re using shared hosting, you’re out of luck.
Hi
NEVER FORGET to assign name to your file upload input. It took 40 days of my time!!!
Worked like a treat! Kudos to you!
Does it work in cross domain also????????????
Thanks, nice works…
Here is server side example. Do not name it index.php. Name it something like “upload_process.php”.
This example shows uploaded file info. and text content.
Thanks, I have lots and lots of IE users ARRRGGG – this works like a charm!
Aren’t the last 2 lines unnecessary? You are setting the iframe’s name in the beginning using iframe.setAttribute(“name”, “upload_iframe”); And why are you getting the iframe by calling document.getElementById? You have it’s link in ‘iframe’ variable already :)
can I use the same in Struts also.
Can you please give me jsp file upload server script instead of php and also tell me that how to set response in jsp and how to receive in html
I try to convert this code to jquery is there anyone that can help me?
Sorry, you have the answer in the comment of Foo Bar it works great in IE/Mozilla/Google Chrome
Sorry, you have the answer in the comment of Foo Bar it works great in IE/Mozilla/Google Chrome
Excellent boss you saved me
Hi i used the above script and it works very well in mozilla but not working at all in IE.
I am getting Access denied error on form.submit().
Below is jsfiddle link resembling my implementation
http://jsfiddle.net/URkbL/1/
Please help..
after uploading file. when i submit form it is opening new window in chrome. please help
working very nice sir,i can’t forget
Hi,
I have an issue, will be very thankful if anyone clarifies it. Am using struts 1.4.
In my screen I have a 1000 rows of records, now i want to submit this page to save 1000 records, so I formed an xml content and through ajax submit I tried to submit, before submit in alert values are displayed but after ajax submit all my form elements are null in my action class. Is there any size limit for ajax submit and my form method is POST.
Thanks,
Raj
Hi, thank you…
Hi, thanks for sharing
a good solution
but, i have problem when i put form upload file in other form
so when finish upload file, action other form be change too
Ex:
after upload, primaryForm be change action to upload.php :(
i want to primaryForm action to regis.php
Please give a solution!
Hi Kendor,
Could you solve your issue? I encountered the issue like you.
I don’t know how to solve this issue.
If you got any idea, please share to me.
Thanks
Win
Hi win naung,
I try more other solutions but not can solve it
so that i split other step
example:
step1->input email, name
step2->upload images
step3->confirm
step4->complete
^_^
When I select a file it gives a permission denied in the following line of code:
// Message from server…
if (iframeId.contentDocument) {
but if i simply click on upload button with out selecting any file it works fine. Sure it is something related to permission issue. I am using Window 7 and VS 2010.
Also it will be very helpfull if someone can share the C# code for the server side as well. I am using ASP.NEt WEB API.
Thanks
Somnath
Please ignore my previous query… I found the issue…. Please share me the server side code in C#. I am using MVC 4 WEB API.
Thanks
Somnath
man that was so smart
final MfTextArea question = new MfTextArea(“question”, new Model(“Your Question”),
new PropertyModel(getItem(), “question”), QUESTION_MAX_LENGTH, true);
question.setCharactersLeftJS(QUESTION_MAX_LENGTH);
question.setRows(10);
question.setCols(50);
question.setMfRendering(false);
form.add(new MfTablelessFieldLayoutBorder(“questionWrapper”, question));
MfFileUploadPanel fileUpload =
new MfFileUploadPanel(“fileUpload”, “Attachments”,
new PropertyModel<List>(getItem(),
“attachments”), false, “”);
fileUpload.setMaxFiles(5);
MfFileContentTypeValidator fileContentTypeValidator = new MfFileContentTypeValidator(
Arrays.asList(allowedAttachments));
fileUpload.addValidator(fileContentTypeValidator);
fileUpload.setVisible(getItem().isAttachmentsAllowed());
fileUpload.setNamingStrategy(getItem().getFileUploadNamingStrategy());
fileUpload.setOutputMarkupPlaceholderTag(true);
fileUpload.setMfRendering(false);
form.add(new MfTablelessFieldLayoutBorder(“fileUploadWrapper”, fileUpload));
buttons = new MfButtonPanel(“submit”, “Continue”);
buttons.setOutputMarkupId(true);
buttonWrapper = new MfTablelessFieldLayoutBorder(“submitWrapper”, buttons);
buttonWrapper.setOutputMarkupId(true);
form.add(buttonWrapper);
questionDiv.add(form);
Multiple attachments/Browse buttons getting displayed on askanewquestion page for ie8… what could be the reason?? any help appreciated.
thanks
I’m sorry Viral, but you will need to update this article, as file uploads using ajax ARE possible now in almost all browsers supporting HTML5 with the XMLHttpRequest2 ‘FormData’ form object.
Start here first for more info…
https://developer.mozilla.org/en-US/docs/Web/API/FormData
Then bop over to StackOverflow for a multitude of questions and answers on using the formdata object in HTML5 to upload files with ajax.
Important that programmers stay up to date, and even though the formdata api in HTML5 is still not officially ratified, all major browsers, including mobile browsers, support it already (I believe at this point in time, only Safari Mini mobile browser is the ONLY one that does not).
With HTML5 and the FormData object, we can now even upload binary data as blobs as well with AJAX. There are even many plugins for this at this point in both vanilla javascript and jquery flavors.
I know this post is older, but felt it necessary to mention this based on the very recent dates on other comments, where the posters are not aware your claim at the head of this article are no longer the case. Uploading binary data (files) with ajax is alive and well, and in heavy use already.
Sorry… One more thing… Gmail has been using flash for their Gmail since 2012, just to let you know!
GMAIL uses Flash to send files because of checking file size before it is sent.
There is no other way (in my knowledge) to check file size or any other params before it is sent to serwer side :)
I was lost. FOR 9 days. you sir are my saviour.
iframeId.parentNode.removeChild giving me issues though?
how can I display the uploaded files in a div on the same page
Thanks Patel! Works Perfectly! You Save My DAY :) Thank you SOMUCH.
Nice Viral Patel its is perfect example of file uploading process without refreashing page. I want to say thanks from core of my heart. Its amazing. WoW :-)
Thanks…
very helpfull post
Hi,
I found this post really helpful.But I am facing some issues in here.After file upload the message inside the is not getting cleared.Also the the filename selected earlier is also not getting cleared when we click for a new upload.
Thanks for the tutorial. I tried the above solution in IE8 and it is throwing “Access is denied” error
Amazing, it works :). Thanks
Awesome solution, It worked for me. I faught a lot to achieve file upload functionality in modal dialog and finally got this solution.Thanks Very Much.
Thnks But How to create scripted server to recieve uploads….??
What’s the difference between ‘iframe’ and ‘iframeId’ variables? I guess they are same.
It worked very well for me !! Thank you very much :D
You are just great man, Tell me how can I cad image path in it. I am already echo in php
Amazing, works as-is!! Great!!!
Nice work but you made it more complicated! It could be simplified a lot
Nelson – Can you provide a working version of a simplified script?
Hi Viral
Thanks for uploading this post.
really very helpful
I have integrated the code u provided but on IE8 it is returning a download pop up which contains json response which currenly we are printing on page.
is there any workaround to prevent this pop up.
What is the Controller logic?
Can I use like this?
@RequestMapping(value={“index.php”}, method=RequestMethod.POST)
public @ResponseBody boolean optInFileUpload(CommonsMultipartFile file){…. }
This is brilliant, and it beats the heck out of AJAX. Thank you kind sir !!!
Is there a way to get a response back using this method ?
Hey Thanks,
Nice article. I was searching from many hours to find the solution provided here.
Thanks
How does the data from the server side retrieves to above mention page after the form is submitted.
I am rendering some json data
Suppose {a:1,b:12,c:13}. How to show these data in readable form.
Finally! Thaaaaaank you very much, it was what I wanted, and surprisingly without ajax.