0

Possible Duplicate:
Ajax file upload

Is it possible to write an ajax request with JQuery that "submits" a form with a file field? I want to do it because in this way i can make the user upload a file without leaving the current page.

How should i write the $.ajax() call? and in particular how should i set into the ajax call the file field?

EDIT: I'd like to use only core JQuery functions, without plugins.

Thanks.

Community
  • 1
  • 1
fat
  • 5,098
  • 4
  • 28
  • 31
  • A simple search in google.com or even here for "jQuery Ajax Upload" brings a lot of content. See: http://stackoverflow.com/questions/2751795/ajax-file-upload –  Dec 06 '11 at 13:17
  • Maybe i should be more specific. I have a hidden
    tag that holds the upload form. Since i am not using frames and i don't want to, i need (if possible) to send the form (through POST) without refreshing the page.
    – fat Dec 06 '11 at 13:18

2 Answers2

2

To maintain compatibility with the widest range of browsers this needs to be done through a hidden iframe.

Here is some sample code to demonstrate what I mean:

<iframe name="my_iframe" id="my_iframe" style="display: none;"></iframe>

<form action="/next_step.php" method="post" target="my_iframe" entype="multipart/form-data">
    <input type="file" name="file_upload" />
    <input type="button" id="upload_btn" value="Upload file" />

    <label for="name">Name</label>
    <input type="text" name="name" id="name" />

    <input type="submit" value="Next step" />
</form>

<script type="text/javascript">
    var btn = document.getElementById('upload_btn');
    btn.onclick = function(){
        var form_elem = document.forms[0];
        // direct file uploads through the hidden iframe
        form_elem.action = '/test_file.php';
        form_elem.target = 'my_iframe';

        // submit the form
        form_elem.submit();
    };
</script>

There are projects out there that make this easier such as Plupload

Treffynnon
  • 21,365
  • 6
  • 65
  • 98
  • looks like i cant escape from frames.. – fat Dec 06 '11 at 13:20
  • I tried with this solution but while request call status will come cancelled. In database record is created with empty data.any suggestions? – stefun Jan 08 '14 at 16:01
  • @stefun Something has to happen to cancel it, such as leaving the page or submitting another file to the same iframe. – Kevin B Jan 08 '14 at 16:33
0

It's hard. You're much better to use a service that already exists.

I did this recently on a site because I needed the whole form to submit without reloading the page, and uploadify was the best AJAX file uploader I found.

Nick
  • 6,316
  • 2
  • 29
  • 47
  • is there any plugin that shows progress bar **without using flash**? – Anupam Dec 06 '11 at 13:38
  • I wouldn't think so. As far as I know Flash is used to keep the connection open between the file upload+the server, which is required for larger files (i.e. files that you would want a progress bar on). – Nick Dec 06 '11 at 14:09
  • i was wondering if [dwr](http://directwebremoting.org/dwr/index.html) can do that. They had a file upload demo there with progress bar – Anupam Dec 17 '11 at 15:38
  • I found [this jquery plugin](http://pixelcone.com/fileuploader/) which uploads with progress bar without using flash – Anupam Dec 20 '11 at 11:43