您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“如何使用Html5實現(xiàn)異步上傳文件”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“如何使用Html5實現(xiàn)異步上傳文件”吧!
服務(wù)器準備IIS
需要設(shè)置IIS里面的HTTP響應標頭,如圖添加如下設(shè)置,添加這項“Access-Control-Allow-Origin”,只有添加了這行才能支持跨域,不然像Chrome瀏覽器會報錯
頁面代碼:
XML/HTML Code復制內(nèi)容到剪貼板
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
<title>Html5上傳文件</title>
</head>
<body>
<div class="app">
<h2>Html5上傳文件測試,帶進度條</h2>
<div>
<input type="file" value="" id="fileInput" name="files" onchange="fileSelected()" />
<div style="margin:30px;">
<input type="button" value="上傳" onclick="uploadFile()" />
</div>
<div style="margin:30px;">
<div id="fileName"></div>
<div id="fileSize"></div>
<div id="fileType"></div>
</div>
<div style="margin:30px;width:500px;height:15px;border:1px solid #aeaeae;">
<div id="progress" style="background:#4cff00;height:15px;width:0%;"></div>
<div id="percentNumber"></div>
</div>
<div style="margin:30px;">
<div id="msg"></div>
</div>
</div>
</div>
<script type="text/javascript">
function fileSelected() {
//重置狀態(tài)顯示
document.getElementById("msg").innerHTML = "";
document.getElementById('percentNumber').innerHTML = '';
document.getElementById("progress").style.width = "0%";
var file = document.getElementById('fileInput').files[0];
if (file) {
var fileSize = 0;
if (file.size > 1024 * 1024)
fileSize = (Math.round(file.size * 100 / (1024 * 1024)) / 100).toString() + 'MB';
else
fileSize = (Math.round(file.size * 100 / 1024) / 100).toString() + 'KB';
document.getElementById('fileName').innerHTML = 'Name: ' + file.name;
document.getElementById('fileSize').innerHTML = 'Size: ' + fileSize;
document.getElementById('fileType').innerHTML = 'Type: ' + file.type;
}
}
function uploadFile() {
var fd = new FormData();
fd.append("fileInput", document.getElementById('fileInput').files[0]);
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", "http://10.0.0.200:9001/Home/Upload");//修改為自己服務(wù)器接口地址
//xhr.setRequestHeader("Access-Control-Allow-Origin", "*");//需要在IIS里面配置,就可以跨域請求了
//xhr.setRequestHeader("Content-Type", "multipart/form-data");
xhr.send(fd);
}
function uploadProgress(evt) {
if (evt.lengthComputable) {
var percentComplete = Math.round(evt.loaded * 100 / evt.total);
document.getElementById('percentNumber').innerHTML = percentComplete + '%';
var jindutiao = document.getElementById("progress");
jindutiao.style.width = percentComplete + "%";
}
else {
document.getElementById('percentNumber').innerHTML = '不支持進度計算';
}
}
function uploadComplete(evt) {
//evt.target.responseText
document.getElementById("msg").innerHTML = "上傳成功";
}
function uploadFailed(evt) {
document.getElementById("msg").innerHTML = "上傳過程中有一個錯誤";
}
function uploadCanceled(evt) {
document.getElementById("msg").innerHTML = "用戶取消了上傳或者瀏覽器刪除了連接";
}
</script>
</body>
</html>
到此,相信大家對“如何使用Html5實現(xiàn)異步上傳文件”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學習!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。