JavaScript文件上傳的常見問題整理

文件上傳:<input type="file" /> (IE9及以下不支持下面這些功能,其它瀏覽器最新版本均已支持。)

1、允許上傳文件數量

允許選擇多個文件:

<input type="file" multiple>

隻允許上傳一個文件:

<input  type="file" single>

2、上傳指定的文件格式

<input type="file" accept="image/gif,image/png" />

這裡的上傳格式僅指打開上傳彈窗選擇文件時默認的文件格式,可手動修改成其它文件格式。

如果不限制上傳圖片的格式可寫成:accept="image/*"

其它格式參考如下:

*.3gpp   audio/3gpp, video/3gpp    3GPP Audio/Video
*.ac3    audio/ac3    AC3 Audio
*.asf    allpication/vnd.ms-asf    Advanced Streaming Format
*.au     audio/basic    AU Audio
*.css    text/css    Cascading Style Sheets
*.csv    text/csv    Comma Separated Values
*.doc    application/msword    MS Word Document
*.dot    application/msword    MS Word Template
*.dtd    application/xml-dtd    Document Type Definition
*.dwg    image/vnd.dwg    AutoCAD Drawing Database
*.dxf    image/vnd.dxf    AutoCAD Drawing Interchange Format
*.gif    image/gif    Graphic Interchange Format
*.htm    text/html    HyperText Markup Language
*.html   text/html    HyperText Markup Language
*.jp2    image/jp2    JPEG-2000
*.jpe    image/jpeg    JPEG
*.jpeg   image/jpeg    JPEG
*.jpg    image/jpeg    JPEG
*.js     text/javascript, application/javascript    JavaScript
*.json   application/json    JavaScript Object Notation
*.mp2    audio/mpeg, video/mpeg    MPEG Audio/Video Stream, Layer II
*.mp3    audio/mpeg    MPEG Audio Stream, Layer III
*.mp4    audio/mp4, video/mp4    MPEG-4 Audio/Video
*.mpeg   video/mpeg    MPEG Video Stream, Layer II
*.mpg    video/mpeg    MPEG Video Stream, Layer II
*.mpp    application/vnd.ms-project    MS Project Project
*.ogg    application/ogg, audio/ogg    Ogg Vorbis
*.pdf    application/pdf    Portable Document Format
*.png    image/png    Portable Network Graphics
*.pot    application/vnd.ms-powerpoint    MS PowerPoint Template
*.pps    application/vnd.ms-powerpoint    MS PowerPoint Slideshow
*.ppt    application/vnd.ms-powerpoint    MS PowerPoint Presentation
*.rtf    application/rtf, text/rtf    Rich Text Format
*.svf    image/vnd.svf    Simple Vector Format
*.tif    image/tiff    Tagged Image Format File
*.tiff   image/tiff    Tagged Image Format File
*.txt    text/plain    Plain Text
*.wdb    application/vnd.ms-works    MS Works Database
*.wps    application/vnd.ms-works    Works Text Document
*.xhtml  application/xhtml+xml    Extensible HyperText Markup Language
*.xlc    application/vnd.ms-excel    MS Excel Chart
*.xlm    application/vnd.ms-excel    MS Excel Macro
*.xls    application/vnd.ms-excel    MS Excel Spreadsheet
*.xlt    application/vnd.ms-excel    MS Excel Template
*.xlw    application/vnd.ms-excel    MS Excel Workspace
*.xml    text/xml, application/xml    Extensible Markup Language
*.zip    aplication/zip    Compressed Archive

3、FileList上傳文件數組

FileList對象都是一組文件對象的集合,而文件對象則擁有下列的屬性:
name – 文件名(不包含路徑)
type – 文件的MIME類型(小寫)
size – 文件的尺寸(單位為字節)

lastModifiedDate 為上傳文件的最後修改時間

通過上面的幾個對象,我們能夠控制用戶上傳的文件大小和文件類型,以便減輕服務器再次檢測時的壓力,並提升安全系數

var a=document.getElementById("file");
a.onchange=function(e){
    e=e || window.event;
    var b=e.target.files;
    alert(b[0].name);
}

4、拖動上傳

dataTransfer 對象

dropEffect[=sCursorStyle]

設置或獲取拖拽操作的類型和要顯示的光標類型

  • copy:復制樣式被顯示
  • link:鏈接樣式被顯示
  • move:移動樣式被顯示
  • none:默認,沒有鼠標樣式被定義

effectAllowed[=sEffect]
設置或獲取數據傳送操作可應用與該對象的源元素

  • copy 選項被復制
  • link 選項被dataTransfer作為link方式保存
  • move 當放置時,對象被移動至目標對象
  • copylink 選項是被復制還是被作為link方式保存關鍵在於目標對象
  • linkmove 選項是被作為link方式保存還是被移動關鍵在於目標對象
  • all 所有效果都被支持
  • none 不支持任何效果
  • uninitialized 默認不能通過這個屬性傳遞任何值

setData(sFormat,sData) 將指定格式的數據賦值給dataTransfer或者clipboardData sFormat:URL,Text
getData(sFormat) 從dataTransfer或者clipboardData中獲取值 sFormat:URL,Text
clearData([sFormat]) 通過dataTransfer或者clipboardData中刪除某種格式的數據 sFormat:Text,URL,File,HTML,Image

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
#DropZone{border:1px solid #CCC;height:50px;}
</style>
</head>
<body>
<input type="file" multiple accept="image/gif,image/png" id="file">
<div id="DropZone"></div>
<div id="Lists"></div>
<script>
var aa=document.getElementById("file");
aa.onchange=function(){
    fileDrop();
}
  
function fileDrop(e) {
    e = e || window.event;
      
    e.stopPropagation(); // 阻止冒泡
    e.preventDefault();  //阻止默認行為
      
    var files = e.dataTransfer.files;   //FileList
      
    var output = [];
      
    for(var i = 0, f; f = files[i]; i++) {
        output.push('<li><strong>' + f.name + '</strong>(' + f.type + ') - ' + f.size +' bytes</li>');
    }
      
    document.getElementById('Lists').innerHTML = '<ul>' + output.join('') + '</ul>';
};
  
function dragOver(e) {
    e = e || window.event;
      
    e.stopPropagation();
    e.preventDefault();
    e.dataTransfer.dropEffect = 'copy'; //指定拖放視覺效果
};
var d = document.getElementById('DropZone');
  
try {
    d.addEventListener('dragover', dragOver, false);
    d.addEventListener('drop', fileDrop, false)
} catch(ex) {
    document.write('something must be wrong!');
}
  
</script>
</body>
</html>

5、FileReader文件預覽,讀取文件數據(同時可讀取圖片文件寬高)

FileReader包含四種異步讀取文件的方式:

  • FileReader.readAsBinaryString(Blob|File) – result屬性包含的是file/blob的二進制字符串形式的數據。每個字節由一個0-255的整數表示。
  • FileReader.readAsText(Blob|File, opt_encoding) – result屬性包含的是以文本方式表示的file/blob數據。默認情況下,字符串以'UTF-8'編碼方式解碼。使用opt_encoding參數可以指定一個不同的格式。
  • FileReader.readAsDataURL(Blob|File) – result屬性包含的是以data URL編碼的file/blob數據。
  • FileReader.readAsArrayBuffer(Blob|File) – result屬性包含的是以ArrayBuffer對象表示的file/blob數據。

一旦這些read方法被調用,onloadstart, onprogress, onload, onabort, onerror, onloadend就可以被用來追蹤進度。

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
body{margin:0;padding:0;}
img{vertical-align:bottom;}
</style>
</head>
<body>
<input type="file" multiple accept="image/gif,image/png,,image/jpeg" id="Files">
<div id="Preview"></div>
<ul id="Errors"></ul>
<script>
function fileSelect(e) {
    e = e || window.event;
      
    var files = e.target.files;  //FileList Objects   
    var ireg = /image\/.*/i,
        p = document.getElementById('Preview');
          
    var ul = document.getElementById('Errors');
    for(var i = 0, f; f = files[i]; i++) {
        if(!f.type.match(ireg)) {
            //設置錯誤信息
            var li = document.createElement('li');
            li.innerHTML = '<li>' + f.name +'不是圖片文件.</li>';
              
            ul.appendChild(li);
              
            continue;
        }
          
        var reader = new FileReader();
          
        reader.onload = (function(file) {
            return function(e) {
                var span = document.createElement('span');
                var img = new Image;
                img.alt=file.name;
                img.onload = function() {
                    console.log(img.height); // image is loaded; sizes are available
                };
  
                img.src=this.result;
                span.innerHTML = '<img class="thumb" src="'+%20this.result%20+'" alt="'+ file.name +'" />';
                  
                p.insertBefore(span, null);
            };
        })(f);
        //讀取文件內容
        reader.readAsDataURL(f);
    }
}
      
if(window.File && window.FileList && window.FileReader && window.Blob) {
    document.getElementById('Files').addEventListener('change', fileSelect, false);
} else {
    document.write('您的瀏覽器不支持File Api');
}
</script>
</body>
</html>

上傳txt文件內容預覽(需對<>&'"|等符號進行過濾否則會中斷讀取):

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
body{margin:0;padding:0;}
img{vertical-align:bottom;}
</style>
</head>
<body>
<input type="file" multiple id="Files">
<div id="Preview"></div>
<ul id="Errors"></ul>
<script>
function encodeHTML(source) { 
    //return source;
    return source 
            .replace(/&/g, '&') 
            .replace(/</g, '<') 
            .replace(/>/g, '>')
            .replace(/\"/g, '"');
}; 
function fileSelect(e) { 
    e = e || window.event; 
        
    var files = e.target.files;  //FileList Objects     
    var ireg = /text\/.*/i, 
        p = document.getElementById('Preview'); 
            
    var ul = document.getElementById('Errors'); 
    for(var i = 0, f; f = files[i]; i++) { 
        console.log(f.type); 
        if(!f.type.match(ireg)) { 
            //設置錯誤信息 
            var li = document.createElement('li'); 
            li.innerHTML = '<li>' + f.name +'不是文本文件.</li>'; 
                
            ul.appendChild(li); 
                
            continue; 
        } 
            
        var reader = new FileReader(); 
            
        reader.onload = (function(file) { 
            return function(e) { 
                var div = document.createElement('div'); 
                div.className = "text" 
                div.innerHTML = encodeHTML(this.result); 
                    
                p.insertBefore(div, null); 
            }; 
        })(f); 
        //讀取文件內容 
        reader.readAsText(f); 
    } 
} 
        
if(window.File && window.FileList && window.FileReader && window.Blob) { 
    document.getElementById('Files').addEventListener('change', fileSelect, false); 
} else { 
    document.write('您的瀏覽器不支持File Api'); 
} 
</script>
</body>
</html>

讀取上傳txt指定區域文本內容:

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
body{margin:0;padding:0;}
img{vertical-align:bottom;}
</style>
</head>
<body>
<input type="file" multiple id="Files">
<button type="button" id="buttons" data-start="0" data-end="3">確定</button>
<div id="range"></div>
<div id="content"></div>
<script>
  
function readBlob(start, end) { 
    var files = document.getElementById('Files').files; 
        
    if(!files.length) { 
        alert('請選擇文件'); 
        return false; 
    } 
        
    var file = files[0], 
        start = parseInt(start, 10) || 0, 
        end = parseInt(end, 10) || (file.size - 1); 
            
    var r = document.getElementById('range'), 
        c = document.getElementById('content'); 
            
    var reader = new FileReader(); 
  
    reader.onloadend = function(e) { 
        if(this.readyState == FileReader.DONE) { 
            c.textContent = this.result; 
            r.textContent = "Read bytes: " + (start + 1) + " - " + (end + 1) + " of " + file.size + " bytes"; 
        } 
    }; 
  
    var blob; 
         
    blob = file.slice(start, end + 1, 'text/plain;charset=UTF-8'); 
    reader.readAsBinaryString(blob); 
}; 
    
try { 
    document.getElementById('buttons').addEventListener('click', function(e) { 
        if(e.target.tagName.toLowerCase() == 'button') { 
            var start = e.target.getAttribute('data-start'), 
                end = e.target.getAttribute('data-end'); 
                    
            readBlob(start, end); 
        }   
    }); 
} catch(ex) { 
    alert('something error happens!') 
} 
</script>
</body>
</html>

File接口提供瞭slice方法支持把文件切成不同的片段,第一個參數是起始的字節數,第二個參數是結束的字節數,還有一個可選的內容類型字符串可以作為第三個參數。早期的chrome和firefox版本不支持file.slice 可使用file.webkitSlice和file.mozSlice替代,最新版本均支持file.slice。

6、文件讀取進度

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<form> 
    <fieldset> 
        <legend>分度讀取文件:</legend> 
        <input type="file" id="File" /> 
        <input type="button" value="中斷" id="Abort" /> 
        <p> 
            <label>讀取進度:</label><progress id="Progress" value="0" max="100"></progress> 
        </p> 
        <p id="Status"></p> 
    </fieldset> 
</form> 
  
<script>
var h = { 
    init: function() { 
        var me = this; 
            
        document.getElementById('File').onchange = me.fileHandler; 
        document.getElementById('Abort').onclick = me.abortHandler; 
            
        me.status = document.getElementById('Status'); 
        me.progress = document.getElementById('Progress'); 
        me.percent = document.getElementById('Percent'); 
            
        me.loaded = 0; 
        //每次讀取1M 
        me.step = 1024 * 1024; 
        me.times = 0; 
    }, 
    fileHandler: function(e) { 
        var me = h; 
            
        var file = me.file = this.files[0]; 
        var reader = me.reader = new FileReader(); 
            
        // 
        me.total = file.size; 
            
        reader.onloadstart = me.onLoadStart; 
        reader.onprogress = me.onProgress; 
        reader.onabort = me.onAbort; 
        reader.onerror = me.onerror; 
        reader.onload = me.onLoad; 
        reader.onloadend = me.onLoadEnd; 
        //讀取第一塊 
        me.readBlob(file, 0);
    }, 
    onLoadStart: function() { 
        var me = h; 
    }, 
    onProgress: function(e) { 
        var me = h; 
            
        me.loaded += e.loaded; 
        //更新進度條 
        me.progress.value = (me.loaded / me.total) * 100; 
    }, 
    onAbort: function() { 
        var me = h; 
    }, 
    onError: function() { 
        var me = h; 
            
    }, 
    onLoad: function() { 
        var me = h; 
    
        if(me.loaded < me.total) { 
            me.readBlob(me.loaded); 
        } else { 
            me.loaded = me.total; 
        } 
    }, 
    onLoadEnd: function() { 
        var me = h; 
            
    }, 
    readBlob: function(start) { 
        var me = h; 
            
        var blob, 
            file = me.file; 
            
        me.times += 1; 
            
        if(file.slice) { 
            blob = file.slice(start, start + me.step + 1); 
        } else if(file.mozSlice) { 
            blob = file.mozSlice(start, start + me.step + 1); 
        } 
            
        me.reader.readAsText(blob); 
    }, 
    abortHandler: function() { 
        var me = h; 
            
        if(me.reader) { 
            me.reader.abort(); 
        } 
    } 
}; 
    
h.init(); 
</script>
</body>
</html>

本文代碼以chrome測試為主。

到此這篇關於JavaScript文件上傳問題整理的文章就介紹到這瞭。希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: