如何異步上傳文件? - How can I upload files asynchronously?

問題:

I would like to upload a file asynchronously with jQuery. 我想用jQuery異步上傳文件。 This is my HTML: 這是個人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代碼: ajax

$(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? 我該怎麼作才能解決此問題? 異步


解決方案:

參考一: https://stackoom.com/question/hEz/如何異步上傳文件
參考二: https://oldbug.net/q/hEz/How-can-I-upload-files-asynchronously
相關文章
相關標籤/搜索