1. 程式人生 > >利用HTML5上傳檔案並顯示在前端預覽,以圖片為例

利用HTML5上傳檔案並顯示在前端預覽,以圖片為例

由於專案中有上傳檔案的功能,所以這次單獨拿出來研究研究,我上網查了查,以前都是用iframe,但是自從HTML5出世之後,就可以利用H5的一些特性來上傳檔案了,啥也不說了,我上程式碼了

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>html5實現上傳</title>
    <style type="text/css">
        #result{
            width: 200px;
            height
:112px
; border:1px solid #eee; }
#result img{ width: 200px; } input{ width: 70px; margin-top: 10px; } @-moz-document url-prefix() { input { width:65px; } }/*單獨對火狐進行設定*/
</style> </head> <body>
<div id = "result"></div> <input id="pic" type="file" name = 'pic' accept = "image/*" onchange = "selectFile()"/> <!-- <button onclick = "handIn()">提交</button> --> <script type="text/javascript"> //var files = document.getElementById('pic').files;
var form = new FormData();//通過HTML表單建立FormData物件 var url = '127.0.0.1:8080/' function selectFile(){ var files = document.getElementById('pic').files; console.log(files[0]); if(files.length == 0){ return; } var file = files[0]; //把上傳的圖片顯示出來 var reader = new FileReader(); // 將檔案以Data URL形式進行讀入頁面 console.log(reader); reader.readAsBinaryString(file); reader.onload = function(f){ var result = document.getElementById("result"); var src = "data:" + file.type + ";base64," + window.btoa(this.result); result.innerHTML = '<img src ="'+src+'"/>'; } console.log('file',file); /////////////////// form.append('file',file); console.log(form.get('file')); } //var xhr = new XMLHttpRequest(); // function handIn(){ // console.log(form.get('file')); // xhr.open("post", url, true); // xhr.addEventListener("readystatechange", function() { // var result = xhr; // if (result.status != 200) { //error // console.log('上傳失敗', result.status, result.statusText, result.response); // } // else if (result.readyState == 4) { //finished // console.log('上傳成功', result); // } // }); // }
</script> </body> </html>

結果如下
這裡寫圖片描述

這裡寫圖片描述

下面被我註釋的是通過AJAX上傳檔案,把檔案傳到後臺的程式碼,由於沒有後臺介面供我測試,url也不對,所以只能寫成這樣了
下面附上input中的“type = file” 中accept屬性的一些配置,上傳什麼檔案配置什麼設定都在下邊

Extension MIME Type
.xlsx   application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
.xltx   application/vnd.openxmlformats-officedocument.spreadsheetml.template
.potx   application/vnd.openxmlformats-officedocument.presentationml.template
.ppsx   application/vnd.openxmlformats-officedocument.presentationml.slideshow
.pptx   application/vnd.openxmlformats-officedocument.presentationml.presentation
.sldx   application/vnd.openxmlformats-officedocument.presentationml.slide
.docx   application/vnd.openxmlformats-officedocument.wordprocessingml.document
.dotx   application/vnd.openxmlformats-officedocument.wordprocessingml.template
.xlsm   application/vnd.ms-excel.addin.macroEnabled.12
.xlsb   application/vnd.ms-excel.sheet.binary.macroEnabled.12
/////////////////////////////////////////////////////////////////////////////////////////////////////
字尾名       MIME名稱
*.3gpp    audio/3gpp, video/3gpp
*.ac3    audio/ac3
*.asf       allpication/vnd.ms-asf
*.au           audio/basic
*.css           text/css
*.csv           text/csv
*.doc    application/msword    
*.dot    application/msword    
*.dtd    application/xml-dtd    
*.dwg    image/vnd.dwg    
*.dxf      image/vnd.dxf
*.gif            image/gif    
*.htm    text/html    
*.html    text/html    
*.jp2            image/jp2    
*.jpe       image/jpeg
*.jpeg    image/jpeg
*.jpg          image/jpeg    
*.js       text/javascript, application/javascript    
*.json    application/json    
*.mp2    audio/mpeg, video/mpeg    
*.mp3    audio/mpeg    
*.mp4    audio/mp4, video/mp4    
*.mpeg    video/mpeg    
*.mpg    video/mpeg    
*.mpp    application/vnd.ms-project    
*.ogg    application/ogg, audio/ogg    
*.pdf    application/pdf    
*.png    image/png    
*.pot    application/vnd.ms-powerpoint    
*.pps    application/vnd.ms-powerpoint    
*.ppt    application/vnd.ms-powerpoint    
*.rtf            application/rtf, text/rtf    
*.svf           image/vnd.svf    
*.tif         image/tiff    
*.tiff       image/tiff    
*.txt           text/plain    
*.wdb    application/vnd.ms-works    
*.wps    application/vnd.ms-works    
*.xhtml    application/xhtml+xml    
*.xlc      application/vnd.ms-excel    
*.xlm    application/vnd.ms-excel    
*.xls           application/vnd.ms-excel    
*.xlt      application/vnd.ms-excel    
*.xlw      application/vnd.ms-excel    
*.xml    text/xml, application/xml    
*.zip            aplication/zip    
*.xlsx     application/vnd.openxmlformats-officedocument.spreadsheetml.sheet