Jquery Ajax Upload File (html5 FormData)

How to use Jquery Ajax to upload files with uploading progress


get single file

1
2
3
let data = $('input[tpye=file]')[0].files[0]
let file = new FormData()
file.append('file',data)

pakage uploading progress xhr function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function onprogress(){
var loaded = evt.loaded;
var tot = evt.total;
var per = Math.floor(100*loaded/tot)
console.log(per)
}

function xhr(){
let xhr = $.ajaxSettings.xhr()
if(onprogress && xhr.upload){
xhr.upload.addEventListener("progress" , onprogress, false);
return xhr;
}
}

use $.ajax() to upload

1
2
3
4
5
6
7
8
9
10
11
$.ajax({
url:'url address',
type:'POST',
contentType:false,
processData:false,
data:file,
xhr:xhr,
success:function(res){
//handle funciton
}
})

more about jquery ajax

上一篇
下一篇