Skip to main content
Add back jQuery to title -- before it was confusing as to what tech stack the question was about
Link

How can I upload files asynchronously with jQuery?

I would like to upload a file asynchronously with jQuery.

html:

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

jQuery:

$(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: ");
            }
        });
    });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<span>File</span>
<input type="file" id="file" name="file" size="10"/>
<input id="uploadbutton" type="button" value="Upload"/>
$(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?

I would like to upload a file asynchronously with jQuery.

html:

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

jQuery:

$(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?

I would like to upload a file asynchronously with jQuery.

$(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: ");
            }
        });
    });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<span>File</span>
<input type="file" id="file" name="file" size="10"/>
<input id="uploadbutton" type="button" value="Upload"/>
 

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

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

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:jQuery:

$(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?

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?

I would like to upload a file asynchronously with jQuery.

html:

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

jQuery:

$(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?

Mark HTML block with the right language for highlighting, correct the jQuery project name and remove the code formatting from it, remove the 'solution' (leave that to answer posts)
Source Link
Martijn Pieters
  • 1.1m
  • 313
  • 4.2k
  • 3.4k
Loading
edited tags
Link
Karim
  • 8.6k
  • 3
  • 26
  • 35
Loading
Rollback to Revision 13
Source Link
Kevin B
  • 95k
  • 16
  • 165
  • 183
Loading
added 4 characters in body; edited title
Source Link
Suraj Rao
  • 29.6k
  • 11
  • 95
  • 104
Loading
Rollback to Revision 11
Source Link
Taryn
  • 246.1k
  • 57
  • 368
  • 407
Loading
added 590 characters in body
Source Link
m87
  • 4.5k
  • 3
  • 17
  • 31
Loading
added 2 characters in body
Source Link
starkshang
  • 8.4k
  • 6
  • 44
  • 56
Loading
deleted 4 characters in body
Source Link
Alex
  • 8.6k
  • 6
  • 39
  • 50
Loading
removed tag from title
Link
Umur Kontacı
  • 35.5k
  • 8
  • 74
  • 96
Loading
Notice removed Reward existing answer by scrowler
Bounty Ended with olanod's answer chosen by scrowler
Notice added Reward existing answer by scrowler
Bounty Started worth 50 reputation by scrowler
Notice removed Reward existing answer by lonesomeday
Bounty Ended with olanod's answer chosen by lonesomeday
Notice added Reward existing answer by lonesomeday
Bounty Started worth 150 reputation by lonesomeday
Copy edited.
Source Link
Peter Mortensen
  • 31.3k
  • 22
  • 109
  • 132
Loading
Notice removed Reward existing answer by Madara's Ghost
Bounty Ended with olanod's answer chosen by Madara's Ghost
Notice added Reward existing answer by Madara's Ghost