js拖拽檔案上傳
阿新 • • 發佈:2018-12-23
function uploadFile( fObj ){
var fSize = ( fObj.size / 1024 / 1024 ).toFixed(2),
fName = fObj.name,
fType = fObj.type;
var fd = new FormData();
fd.append( "uploadfile", fObj );
var xhr = new XMLHttpRequest();
xhr.upload.addEventListener("progress", uploadProgress, false);
xhr.addEventListener( "load", uploadComplete, false );
xhr.addEventListener( "error", uploadFailed, false );
xhr.addEventListener( "abort", uploadCanceled, false );
xhr.open( "POST", url, true );
xhr.send( fd );
}
//進度把控
function uploadProgress( evt ){
if( evt.lengthComputable ){
var percentComplete = Math.round( evt.loaded * 100 / evt.total );
console.log( "已上傳:" + percentComplete );
}
}