HTML5 檔案域+FileReader 讀取檔案並上傳到伺服器(三)
阿新 • • 發佈:2018-12-17
一、讀取檔案為blob並上傳到伺服器
HTML
<div class="container"> <!--讀取要上傳的檔案--> <input type="file" id="file" /> <input type="button" id="btn1" value="點選上傳" onclick="uploadClick();" /> </div>
JS
//讀取圖片例項,並上傳到伺服器 var fileBox = document.getElementById('file'); fileBox.onchange= function () { //獲取選擇檔案的陣列 var fileList = fileBox.files; for (var i = 0; i < fileList.length; i++) { var file = fileList[i]; uploadFile(file); } } //關鍵程式碼上傳到伺服器 function uploadFile(file) { var reader = new FileReader(); reader.readAsArrayBuffer(file); reader.onload= function () { var blob = new Blob([reader.result]); //提交到伺服器 var fd = new FormData(); fd.append('file', blob); var ext = file.name.substring(file.name.lastIndexOf('.')); fd.append('extention', ext); fd.append('maxsize', 1024*1024*4);//Asp.net 預設一次上傳最大4MBfd.append('isClip', -1); var xhr = new XMLHttpRequest(); xhr.open('post', '../ashx/upload.ashx', true); xhr.onreadystatechange = function () { if (xhr.readyState == 4 && xhr.status == 200) { var data = eval('(' + xhr.responseText + ')'); console.info(data); if (data.success) { //上傳成功 var imgName = data.msg; alert(imgName); } else { alert(data.msg); } } } //開始傳送 xhr.send(fd); } }
後臺C#處理關鍵程式碼:
//接收檔案 HttpRequest req = _Context.Request; string newname = _fileInfo.CreateNewName(newExtention); //接收二級制資料並儲存 Stream stream = _PostedFile.InputStream; byte[] dataOne = new byte[stream.Length]; stream.Read(dataOne, 0, dataOne.Length); FileStream fs = new FileStream(_fileInfo.TempFile + newname, FileMode.Create, FileAccess.Write); try { fs.Write(dataOne, 0, dataOne.Length); } finally { fs.Close(); stream.Close(); } return newname;
二、讀取圖片檔案,並上傳到伺服器
HTML
<div class="container"> <!--圖片型別驗證方法1--> <input type="file" id="file" accept="image/*" /> <input type="button" id="btn1" value="點選上傳" onclick="uploadClick();" /> <h4>選擇檔案如下:</h4> <img src="" id="img1" /> </div>
JS:
//讀取圖片例項,並上傳到伺服器 var fileBox = document.getElementById('file'); fileBox.onchange = function () { //獲取選擇檔案的陣列 var fileList = fileBox.files; for (var i = 0; i < fileList.length; i++) { var file = fileList[i]; //圖片型別驗證第二種方式 if (/image\/\w+/.test(file.type)) readFile(file); else console.log(file.name + ':不是圖片'); } } //讀取二進位制圖片檔案,並上傳到伺服器 function uploadClick() { var fileList = fileBox.files; for (var i = 0; i < fileList.length; i++) { var file = fileList[i]; //圖片型別驗證第二種方式 if (/image\/\w+/.test(file.type)) uploadFile(file); else console.log(file.name + ':不是圖片'); } } //關鍵程式碼上傳到伺服器 function uploadFile(file) { var reader = new FileReader(); reader.readAsArrayBuffer(file); reader.onload = function () { var blob = new Blob([reader.result], { type: 'image/jpg' }); //提交到伺服器 var fd = new FormData(); fd.append('file', blob); var ext = file.name.substring(file.name.lastIndexOf('.')); fd.append('extention', ext); fd.append('isClip', -1); var xhr = new XMLHttpRequest(); xhr.open('post', '../ashx/upload.ashx', true); xhr.onreadystatechange = function () { if (xhr.readyState == 4 && xhr.status == 200) { var data = eval('(' + xhr.responseText + ')'); console.info(data); if (data.success) { //上傳成功 var imgName = data.msg; alert(imgName); } else { alert(data.msg); } } } //開始傳送 xhr.send(fd); } } //讀取圖片內容 為DataURL function readFile(file) { var reader = new FileReader(); reader.readAsDataURL(file); reader.onload = function (e) { var result = reader.result; $('.container blockquote').text(result); $('#img1').attr('src', result) } }
後臺關鍵程式碼處理同上。