如何使用File APIs来读取文件

兼容性检查

if (window.File && window.FileReader && window.FileList && window.Blob) {
	//支持File APIs
} else {
	//不支持File APIs
}
复制代码

FileReader()

FileReader对象让web应用程序能够异步地读取存储在用户电脑上的文件(或者原始数据缓冲区)的内容。在JavaScript中,FileReaderd对象经过传入两种相应的对象(File和Blob)来进行数据的读取,并且这个方法在Web Workers中也能使用。web

FileReader 包括四个异步读取文件的选项:数组

  • FileReader.readAsBinaryString(Blob|File) - 返回值的result 属性将包含二进制字符串形式的file/blob 数据。每一个字节均由一个 [0..255] 范围内的整数表示。
  • FileReader.readAsText(Blob|File, opt_encoding) - 返回值的result 属性将包含文本字符串形式的 file/blob 数据。该字符串在默认状况下采用“UTF-8”编码。使用可选编码参数可指定其余格式。
  • FileReader.readAsDataURL(Blob|File) - 返回值的result 属性将包含编码为数据网址的 file/blob 数据。
  • FileReader.readAsArrayBuffer(Blob|File) - 返回值的result 属性将包含 ArrayBuffer 对象形式的 file/blob 数据。

FileReader 对象调用其中某一种读取方法后,可以使用 onloadstart、onprogress、onload、onabort、onerror 和 onloadend 跟踪其进度。浏览器

读取文件并显示进度

下面的示例从用户选择的内容中过滤掉了图片,对文件调用 reader.readAsDataURL(),并经过将“src”属性设为数据网址来呈现缩略图。bash

<style>
    .thumb {
        height: 75px;
        border: 1px solid #000;
        margin: 10px 5px 0 0;
    }

    #list {
        border: 1px solid lightgrey;
        padding: 15px;
        text-align: center;
    }

    #progress_bar {
        margin: 10px 0;
        padding: 3px;
        border: 1px solid #000;
        font-size: 14px;
        clear: both;
        opacity: 0;
        -moz-transition: opacity 1s linear;
        -o-transition: opacity 1s linear;
        -webkit-transition: opacity 1s linear;
    }

    #progress_bar.loading {
        opacity: 1.0;
    }

    #progress_bar .percent {
        background-color: #99ccff;
        height: auto;
        width: 0;
    }
</style>


<body>
    <input type="file" name="files[]" id="files" multiple />
    <div id="list"></div>
    <button onclick="abortRead();">Cancel read</button>
    <script>
    let reader;
    let progress;
    let progress_bar;

    function abortRead() {
        reader.abort();
    }

    function errorHandler(evt) {
        let error = evt.target.error;

        switch (error.code) {
            case error.NOT_FOUND_ERR:
                alert('没有找到文件');
                break;
            case error.NOT_READABLE_ERR:
                alert('没法读取文件');
                break;
            case error.ABORT_ERR:
                break;
            default:
                alert('文件读取错误');
        }
    }

    function updateProgress(evt) {
        if (evt.lengthComputable) {
            let percentLoaded = Math.round((evt.loaded / evt.total) * 100);
            
            if (percentLoaded < 100) {
                progress.style.width = percentLoaded + '%';
                progress.textContent = percentLoaded + '%';
            }
        }
    }

    function handleFileSelect(evt) {
        let files = evt.target.files;
        
        //建立进度条
        progress_bar = document.createElement('div');
        progress_bar.id = 'progress_bar';
        progress = document.createElement('div');
        progress.className = 'percent';
        progress.style.width = '0%';
        progress.textContent = '0%';
        progress_bar.appendChild(progress);
        document.getElementById('list').appendChild(progress_bar);

        for (let i = 0; i < files.length; i++) {
            reader = new FileReader();
            if (!files[i].type.match('image.*')) {
                alert('选择的文件不是图片');
                abortRead();
                return;
            }
            reader.onerror = errorHandler;
            reader.onprogress = updateProgress;

            reader.onabort = (e) => {
                alert('文件读取已取消');
            };

            reader.onloadstart = (e) => {
            	progress_bar.className = 'loading';
            };

            reader.onload = (e) => {
            	
            	let span = document.createElement('span');
                span.innerHTML = ['<img class="thumb" src="', e.target.result, '" title="', files[i].name, '"/>'].join('');
                document.getElementById('list').insertBefore(span, progress_bar);
                progress.style.width = '100%';
                progress.textContent = '100%';
            };
            reader.readAsDataURL(files[i]);
        }
    }

    document.getElementById('files').addEventListener('change', handleFileSelect, false);
    </script>
</body>
复制代码

See the Pen FileReader Demo by Lu (@smallbone) on CodePen.服务器

FileList API

字面上能够理解为多个File对象组合成的数组,可是只有length属性item(index)方法,访问其中的File对象既可使用files.item(index),也可使用files[index]的方法。app

File API

File对象是一种特定类型的Blob。FileReader, URL.createObjectURL(), createImageBitmap(), 以及XMLHttpRequest.send() 都接受Blobs和Files。异步

  • File对象包含的信息
{
	lastModified: 1428005315000,
	lastModifiedDate: Thu Apr 02 2015 15:08:35 GMT-0500 (CDT),
	name: "profile.pdf",
	size: 135568,
	type: "application/pdf",
	webkitRelativePath: ""
}
复制代码

须要注意的是,type是根据文件扩展名来判断的,因此并非很可靠。根据上面File对象的信息其实就能够实现一些经常使用的功能了,好比限制文件上传的大小,初步的限制文件上传的类型(固然也能够经过input元素的accept属性来实现,可是最终的类型验证仍是须要在服务器端实现)。ui

File对象通常经过如下途径返回的FileList对象获取:编码

  1. <input type="file">的元素
  2. 拖拽操做的DataTransfer对象
  3. 经过在一个HTMLCanvasElement上调用mozGetAsFile() API

经过input来选择文件

/* 假设input元素为<input type="file" id="upload" multiple> */
//multiple表示一次支持多个文件上传
let uploadInput = document.getElementById('upload');
uploadInput.addEventListener('change', ()=>{
	let fileList = uploadInput.files;
	console.log(fileList);
});
复制代码

因为FileList对象并无forEach()方法,因此通常须要经过for循环来遍历其中的每一个File对象:spa

for (var i = 0; fileCount = fileList.length; i < fileCount; i++) {
	console.log(fileList[i]);
}
复制代码

可是咱们也能够经过其余方式来使用forEach()方法:

//1.call方法
[].forEach.call(fileList, (file, i, fileList)=>{
	...
});

//2.ES6方法
Array.from(uploadInput).forEach((i)=>{
	...
});
复制代码

经过拖拽(drag&drop)选择文件

拖拽事件:

  • drag(开始拖动,持续事件)
  • dragend(释放鼠标或者按下ESC,结束拖动)
  • dragenter(进入有效的拖拽区域时)
  • dragexit(当一个元素再也不是拖动操做的直接选择目标时)
  • dragleave(离开有效的拖拽区域时)
  • dragover(悬停在有效的拖拽区域内时,持续事件)
  • dragstart(开始拖动)
  • drop(目标放置到有效的拖拽区域时)

其中须要注意两点:

  1. 若是dragover事件不阻止默认事件,drop事件就不会被触发。
  2. dragexit和dragleave在不一样浏览器中的触发存在差别,dragexit在Chrome浏览器中就永远不会被触发
//拖拽和显示区域
<div id="drop_zone">Drop files here</div>
<output id="list"></output>

<script>
	function handleFileSelect(evt) {
		evt.stopPropagation();
		evt.preventDefault();
		//注意这里再也不是target.files
		let files = evt.dataTransfer.files;
		let output = [];
		
		[].forEach.call(files, (file)=>{
			output.push('<li><strong>', file.name, '</strong> (', file.type || 'n/a', ') - ', (file.size/1024).toFixed(3), ' Kb, last modified date: ', file.lastModifiedDate.toLocaleDateString(), '</li>');
		});
		document.getElementById('list').innerHTML = '<ul>' + output.join('') + '</ul>';	
	}

	function handleDragOver(evt) {
		evt.stopPropagation();
		evt.preventDefault();
		evt.dataTransfer.dropEffect = 'copy';
	}

	let dropZone = document.getElementById('drop_zone');
	dropZone.addEventListener('dragover', handleDragOver, false);
	dropZone.addEventListener('drop', handleFileSelect, false);
</script>
复制代码
相关文章
相关标签/搜索