Ajax Style File Uploading using Hidden iFrame

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.


<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...
    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...

    document.getElementById(div_id).innerHTML = "Uploading...";

<!-- index.php could be any script server-side for receive uploads. -->
<input type="file" name="datafile" /></br>
<input type="button" value="upload" 
        onClick="fileUpload(this.form,'index.php','upload'); return false;" >
<div id="upload"></div>

Get our Articles via Email. Enter your email address.

You may also like...


  1. Ajay says:

    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.

  2. Nibu says:

    Thanks for the tutorial. I tried the above solution in IE8 and it is throwing “Access is denied” error

  3. Ambika says:

    Amazing, it works :). Thanks

  4. Shantanu says:

    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.

  5. Anurag says:

    Thnks But How to create scripted server to recieve uploads….??

  6. crzhacko says:

    What’s the difference between ‘iframe’ and ‘iframeId’ variables? I guess they are same.

  7. Walid says:

    It worked very well for me !! Thank you very much :D

  8. Umair Hamid says:

    You are just great man, Tell me how can I cad image path in it. I am already echo in php

  9. SMan says:

    Amazing, works as-is!! Great!!!

  10. Nelson Rakson says:

    Nice work but you made it more complicated! It could be simplified a lot

    • rocbar says:

      Nelson – Can you provide a working version of a simplified script?

  11. Shekhar says:

    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.

  12. Murthy says:

    What is the Controller logic?
    Can I use like this?
    @RequestMapping(value={“index.php”}, method=RequestMethod.POST)
    public @ResponseBody boolean optInFileUpload(CommonsMultipartFile file){…. }

  13. Don says:

    This is brilliant, and it beats the heck out of AJAX. Thank you kind sir !!!

  14. Cazz says:

    Is there a way to get a response back using this method ?

  15. Sunil Kumar says:

    Hey Thanks,
    Nice article. I was searching from many hours to find the solution provided here.

  16. Roshan says:

    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.

Leave a Reply

Your email address will not be published. Required fields are marked *