web前端js上传文件
生活随笔
收集整理的這篇文章主要介紹了
web前端js上传文件
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
前端用的js+easyui
Demo:
<html> <head><meta charset="UTF-8"><title>XMLHttpRequest上傳文件</title><script type="text/javascript">//圖片上傳var xhr;//上傳文件方法function UpladFile() {var fileObj = document.getElementById("file").files[0]; // js 獲取文件對(duì)象var url = "http://localhost:8080" + "/api/attachment/upload"; // 接收上傳文件的后臺(tái)地址var form = new FormData(); // FormData 對(duì)象form.append("file", fileObj); // 文件對(duì)象xhr = new XMLHttpRequest(); // XMLHttpRequest 對(duì)象xhr.open("post", url, true); //post方式,url為服務(wù)器請(qǐng)求地址,true 該參數(shù)規(guī)定請(qǐng)求是否異步處理。xhr.onload = uploadComplete; //請(qǐng)求完成xhr.onerror = uploadFailed; //請(qǐng)求失敗xhr.upload.onprogress = progressFunction;//【上傳進(jìn)度調(diào)用方法實(shí)現(xiàn)】xhr.upload.onloadstart = function(){//上傳開(kāi)始執(zhí)行方法ot = new Date().getTime(); //設(shè)置上傳開(kāi)始時(shí)間oloaded = 0;//設(shè)置上傳開(kāi)始時(shí),以上傳的文件大小為0};xhr.send(form); //開(kāi)始上傳,發(fā)送form數(shù)據(jù)}//上傳成功響應(yīng)function uploadComplete(evt) {//服務(wù)斷接收完文件返回的結(jié)果var data = JSON.parse(evt.target.responseText);if(data.success) {alert("上傳成功!");}else{alert("上傳失敗!");}}//上傳失敗function uploadFailed(evt) {alert("上傳失敗!");}//取消上傳function cancleUploadFile(){xhr.abort();}//上傳進(jìn)度實(shí)現(xiàn)方法,上傳過(guò)程中會(huì)頻繁調(diào)用該方法function progressFunction(evt) {var progressBar = document.getElementById("progressBar");var percentageDiv = document.getElementById("percentage");// event.total是需要傳輸?shù)目傋止?jié),event.loaded是已經(jīng)傳輸?shù)淖止?jié)。如果event.lengthComputable不為真,則event.total等于0if (evt.lengthComputable) {//progressBar.max = evt.total;progressBar.value = evt.loaded;percentageDiv.innerHTML = Math.round(evt.loaded / evt.total * 100) + "%";}var time = document.getElementById("time");var nt = new Date().getTime();//獲取當(dāng)前時(shí)間var pertime = (nt-ot)/1000; //計(jì)算出上次調(diào)用該方法時(shí)到現(xiàn)在的時(shí)間差,單位為sot = new Date().getTime(); //重新賦值時(shí)間,用于下次計(jì)算var perload = evt.loaded - oloaded; //計(jì)算該分段上傳的文件大小,單位boloaded = evt.loaded;//重新賦值已上傳文件大小,用以下次計(jì)算//上傳速度計(jì)算var speed = perload/pertime;//單位b/svar bspeed = speed;var units = 'b/s';//單位名稱(chēng)if(speed/1024>1){speed = speed/1024;units = 'k/s';}if(speed/1024>1){speed = speed/1024;units = 'M/s';}speed = speed.toFixed(1);//剩余時(shí)間var resttime = ((evt.total-evt.loaded)/bspeed).toFixed(1);time.innerHTML = ',速度:'+speed+units+',剩余時(shí)間:'+resttime+'s';if(bspeed==0) time.innerHTML = '上傳已取消';}</script> </head> <body> <progress id="progressBar" value="0" max="100" style="width: 300px;"></progress> <span id="percentage"></span><span id="time"></span> <br /><br /> <input type="file" id="file" name="myfile" /> <input type="button" onclick="UpladFile()" value="上傳" /> <input type="button" onclick="cancleUploadFile()" value="取消" /> </body> </html>最初的jsp:
點(diǎn)擊上傳后:
OK~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
總結(jié)
以上是生活随笔為你收集整理的web前端js上传文件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Linux 块设备,Block Laye
- 下一篇: 电脑连接wifi无法使用浏览器,但是可以