我已經創建了一個HTML表單元素,如下所示,用於文件上傳。目前該文件可以正確上傳到服務器。如何從HTML表單獲取數據發佈進度
<form id="file_upload_form" action="http://localhost:8000/v1/file?op=upload" enctype="multipart/form-data" method="post">
<input type="file" name="xxx">
<input type="submit" value="Send">
</form>
我想通過純jQuery/JS獲取文件上傳進度。我從我的研究中得到了一些如下代碼。
$("#file_upload_form").submit(function(){
console.log("here");
var xhr = new XMLHttpRequest();
xhr.upload.addEventListener("progress", function (evt)
{
if(evt.lengthComputable)
{
var progressPercent = (evt.loaded/evt.total) * 100;
console.log(progressPercent);//your function.
}
}, false);
});
但程序一直沒有碰上事件監聽。我的HTML表單可以通過純jQuery/JS獲得文件上傳進度嗎?
入住這 http://stackoverflow.com/questions/15410265/file-upload-progress-bar-with-jquery – Kavin
感謝@Kavin的建議。我在問這個問題之前檢查了這個鏈接。他們的回答不適合我的情況。你會在這裏提供一些基於我的代碼的建議嗎? – ybdesire