Home How can I upload files asynchronously?
Reply: 0

How can I upload files asynchronously?

user11124
1#
user11124 Published in September 19, 2018, 7:23 pm

I would like to upload a file asynchronously with jQuery. This is my HTML:

<span>File</span>
<input type="file" id="file" name="file" size="10"/>
<input id="uploadbutton" type="button" value="Upload"/>

And here my Jquery code:

$(document).ready(function () {
    $("#uploadbutton").click(function () {
        var filename = $("#file").val();

        $.ajax({
            type: "POST",
            url: "addFile.do",
            enctype: 'multipart/form-data',
            data: {
                file: filename
            },
            success: function () {
                alert("Data Uploaded: ");
            }
        });
    });
});

Instead of the file being uploaded, I am only getting the filename. What can I do to fix this problem?

Current Solution

I am using the jQuery Form Plugin to upload files.

share|improve this question
  • 70
    you are only getting the file name because your var filename is getting the value of $('#file'), not the file that lies in the input – Jimmy Nov 3 '09 at 16:01
  • 21
    Here's a good one: http://blueimp.github.io/jQuery-File-Upload/ - HTML5 ajax uploading - Graceful fallback to iframes for unsupported browsers - Multi-file async upload We've used it and it works great. (Documentation here) – Ashish Panery Apr 12 '13 at 18:35
  • jQuery Form Plugin seems the most simple-to-use and cross-browser compatible way... am I right ? – Lyth Sep 18 '13 at 12:32
  • 3
    Check also this: stackoverflow.com/questions/6974684/…, here it explains how to achieve it via jQuery – Chococroc Jan 15 '14 at 12:09
  • 2
    @Jimmy How would he get the get the file that lies in the input instead? – alex Dec 5 '14 at 18:05

30 Answers 30

active oldest votes
up vote 88 down vote
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.484048 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO