Using the File API, which was added to the DOM in HTML5, it's now possible for web content to ask the user to select local files and then read the contents of those files. This selection can be done by either using an HTML <input> element or by drag and drop.

我们可以使用在HTML5中被加入DOM的File API,使得用户选择本地文件并读取这些文件的内容变为可能。这种选择同样可以使用 HTML 的input元素或者通过drag 和 drop来实现。

If you want to use the DOM File API from extensions or other browser chrome code, you can; however, note there are some additional features to be aware of. See Using the DOM File API in chrome code for details.

如果你想通过拓展或者其它的chrome源码浏览器(browser chrome code)使用DOM File API,是可行的;然而,注意有一些附加特性需要知道。看 Using the DOM File API in chrome code 了解详情。



<input type="file" id="input">

The File API makes it possible to access a FileList containing File objects representing the files selected by the user.

File API使访问包含 File 对象的 FileList成为可能,FileList代表被用户选择的文件。

If the user selects just one file, it is then only necessary to consider the first file of the list.


Accessing one selected file using a classical DOM selector:


var selectedFile = document.getElementById('input').files[0];

Accessing selected file(s) on a change event


It is also possible (but not mandatory) to access the FileList through the change event:

可以通过change事件访问 FileList(但不是强制的)。

<input type="file" id="input" onchange="handleFiles(this.files)">

When the user selects a file, the handleFiles() function gets called with a FileList object containing File objects representing the files selected by the user.

当用户选择一个文件时,handleFiles()方法会被调用,同时传入包含File对象的 FileList 对象。File对象代表被用户选择的文件。

If you want to let the user select multiple files, simply use the multiple attribute on the input element:


<input type="file" id="input" multiple onchange="handleFiles(this.files)">

In this case, the file list passed to the handleFiles() function contains one File object for each file the user selected.

在这个例子中,被传入handleFiles()方法的文件列表包含一组代表用户所选文件的 File 对象。

Dynamically adding a change listener



You need to use EventTarget.addEventListener() to add the change event listener, like this:


var inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  var fileList = this.files; /* now you can work with the file list */

Note that in this case, the handleFiles() function itself is the event handler, unlike previous examples where it was called by an event handler which passed it a parameter.

注意在这个例子里,handleFiles() 方法本事是一个事件处理器,不像之前的例子中,它被事件处理器调用然后传递给它一个参数。

Getting information about selected file(s)


The FileList object provided by the DOM lists all of the files selected by the user, each specified as a File object. You can determine how many files the user selected by checking the value of the file list's length attribute:

FileList 对象由DOM提供,列出了所有用户选择的文件,每一个代表了一个 File 对象。你可以通过检查文件列表的length属性决定用户可以选则多少文件

var numFiles = files.length;

Individual File objects can be retrieved by simply accessing the list as an array:


for (var i = 0, numFiles = files.length; i < numFiles; i++) {
  var file = files[i];

This loop iterates over all the files in the file list.


There are three attributes provided by the File object that contain useful information about the file.

这是 File 对象提供的三个属性,包含了关于文件的一些有价值的信息。

The file's name as a read-only string. This is just the file name, and does not include any path information.
The size of the file in bytes as a read-only 64-bit integer.
The MIME type of the file as a read-only string or "" if the type couldn't be determined.
文件的MIME type,只读字符串,当类型不能确定时为""。

Example: Showing file(s) size


The following example shows a possible use of the size property:


<!DOCTYPE html>
<meta charset="UTF-8">
<title>File(s) size</title>
function updateSize() {
  var nBytes = 0,
      oFiles = document.getElementById("uploadInput").files,
      nFiles = oFiles.length;
  for (var nFileId = 0; nFileId < nFiles; nFileId++) {
    nBytes += oFiles[nFileId].size;
  var sOutput = nBytes + " bytes";
  // optional code for multiples approximation
  for (var aMultiples = ["KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"], nMultiple = 0, nApprox = nBytes / 1024; nApprox > 1; nApprox /= 1024, nMultiple++) {
    sOutput = nApprox.toFixed(3) + " " + aMultiples[nMultiple] + " (" + nBytes + " bytes)";
  // end of optional code
  document.getElementById("fileNum").innerHTML = nFiles;
  document.getElementById("fileSize").innerHTML = sOutput;

<body onload="updateSize();">
<form name="uploadForm">
<p><input id="uploadInput" type="file" name="myFiles" onchange="updateSize();" multiple> selected files: <span id="fileNum">0</span>; total size: <span id="fileSize">0</span></p>
<p><input type="submit" value="Send file"></p>

Using hidden file input elements using the click() method

通过click()方法使用隐藏的file input元素

Starting in Gecko 2.0 (Firefox 4 / Thunderbird 3.3 / SeaMonkey 2.1), you can hide the admittedly ugly file <input> element and present your own interface for opening the file picker and displaying which file or files the user has selected. You can do this by styling the input element with display:none and calling the click() method on the <input> element.

从Gecko 2.0 (Firefox 4 / Thunderbird 3.3 / SeaMonkey 2.1)开始,你可以隐藏丑陋的 file <input> 元素然后使用你自己的接口打开文件选择器,然后显示哪个文件(或多个)被用户选择了。你可以通过给input元素添加display:none的样式然后在 <input> 元素上调用 click()方法。

Consider this HTML:


<input type="file" id="fileElem" multiple accept="image/*" style="display:none" onchange="handleFiles(this.files)">
<a href="#" id="fileSelect">Select some files</a>

The code that handles the click event can look like this:


var fileSelect = document.getElementById("fileSelect"),
  fileElem = document.getElementById("fileElem");

fileSelect.addEventListener("click", function (e) {
  if (fileElem) {;
  e.preventDefault(); // 避免导航到 "#"
}, false);

You can style the new button for opening the file picker as you wish.


Using a label element to trigger a hidden file input element

使用label元素来触发一个隐藏的file input元素

To allow opening the file picker without using JavaScript (the click() method), a <label> element can be used.

如果不想使用JavaScript (click() 方法)来打开文件选择器,可以使用 <label> 元素。

Consider this HTML:


<input type="file" id="fileElem" multiple accept="image/*" style="display:none" onchange="handleFiles(this.files)">
<label for="fileElem">Select some files</label>

There is no need to add JavaScript code to call Also in this case you can style the label element as you wish.


Selecting files using drag and drop


You can also let the user drag and drop files into your web application.


The first step is to establish a drop zone. Exactly what part of your content will accept drops may vary depending on the design of your application, but making an element receive drop events is easy:


var dropbox;

dropbox = document.getElementById("dropbox");
dropbox.addEventListener("dragenter", dragenter, false);
dropbox.addEventListener("dragover", dragover, false);
dropbox.addEventListener("drop", drop, false);

In this example, we're turning the element with the ID dropbox into our drop zone. This is done by adding listeners for the dragenter, dragover, and drop events.

在这个例子中,我们将ID为dropbox的元素变为了我们的drop区域。这个只需要给元素添加dragenter, dragover, 和drop 事件监听器就完成。

We don't actually need to do anything with the dragenter and dragover events in our case, so these functions are both simple. They just stop propagation of the event and prevent the default action from occurring:

我们其实并不需要对dragenter and dragover 事件进行处理,所以这些函数都很简单。他们只需要包括禁止事件传播和阻止默认事件。

function dragenter(e) {

function dragover(e) {

The real magic happens in the drop() function:


function drop(e) {

  var dt = e.dataTransfer;
  var files = dt.files;


Here, we retrieve the dataTransfer field from the event, pull the file list out of it, and then pass that to handleFiles(). From this point on, handling the files is the same whether the user used the input element or drag and drop.

这里,我们从事件中获取到了dataTransfer 这个域,然后从中得到文件列表,再将他们传递给handleFiles函数。从这一点开始,处理文件的方法和用input或者用拖拽是一样的了。

Example: Showing thumbnails of user-selected images


Let's say you're developing the next great photo-sharing website and want to use HTML5 to display thumbnail previews of images before the user actually uploads them. You can establish your input element or drop zone as discussed previously and have them call a function such as the handleFiles() function below.


function handleFiles(files) {
  for (var i = 0; i < files.length; i++) {
    var file = files[i];
    var imageType = /^image\//;
    if (!imageType.test(file.type)) {
    var img = document.createElement("img");
    img.file = file;
    preview.appendChild(img); // Assuming that "preview" is the div output where the content will be displayed.
    var reader = new FileReader();
    reader.onload = (function(aImg) { return function(e) { aImg.src =; }; })(img);

Here our loop handling the user-selected files looks at each file's type attribute to see if it's an image file (by doing a regular expression match on the MIME type string pattern "image/*"). For each file that is an image, we create a new img element. CSS can be used to establish any pretty borders or shadows and to specify the size of the image, so that doesn't need to be done here.

这里我们循环处理用户选择的文件,看每个文件的type属性是不是image(通过正则表达式来匹配MIME类型字符串模式"image/*")。 对每个文件而言,如果它是图片,我们就创建一个img元素。可以使用css来创建一个漂亮的边框或阴影来显示图片的具体大小,这儿就不具体做了。

Each image has the CSS class obj added to it, making it easy to find in the DOM tree. We also add a file attribute to each image specifying the File for the image; this will let us fetch the images for actual upload later. We use Node.appendChild() to add the new thumbnail to the preview area of our document.

为了在DOM树中更容易地找到他们,每个图片元素都被添加了一个名为obj的class。我们还给每个图片添加了file属性使它具有 File;这样做可以让我们拿到稍后需要实际上传的图片。我们在预览页中使用 Node.appendChild()来添加新的缩略图。

Next, we establish the FileReader to handle asynchronously loading the image and attaching it to the img element. After creating the new FileReader object, we set up its onload function and then call readAsDataURL() to start the read operation in the background. When the entire contents of the image file are loaded, they are converted into a data: URL which is passed to the onload callback. Our implementation of this routine sets the img element's src attribute to the loaded image which results in the image appearing in the thumbnail on the user's screen.

接下来,我们创建了FileReader来处理异步的图片加载并把他赋给图片元素。在创建一个新的 FileReader对象后,我们新建了它的onload 函数,然后调用readAsDataURL函数在后台开始读取文件的操作。当整个图片文件被全部加载完后,他们被转换成了一个被传递到onload回调函数的data:URL。我们再执行常规操作将img元素的src属性设置为刚刚加载完毕的URL,使得图像可以显示在用户屏幕上的缩略图中。

Using object URLs


Gecko 2.0 (Firefox 4 / Thunderbird 3.3 / SeaMonkey 2.1) introduces support for the DOM window.URL.createObjectURL() and window.URL.revokeObjectURL() methods. These let you create simple URL strings that can be used to reference any data that can be referred to using a DOM File object, including local files on the user's computer.

Gecko 2.0 (Firefox 4 / Thunderbird 3.3 / SeaMonkey 2.1)引入了对DOM window.URL.createObjectURL()window.URL.revokeObjectURL() 方法的支持。这使得你可以创建用于引用任何数据的简单URL字符串,也可以引用一个包括用户电脑上的本地文件的DOM File对象。

When you have a File object you'd like to reference by URL from HTML, you can create an object URL for it like this:


var objectURL = window.URL.createObjectURL(fileObj);

The object URL is a string identifying the File object. Each time you call window.URL.createObjectURL(), a unique object URL is created even if you've created an object URL for that file already. Each of these must be released. While they are released automatically when the document is unloaded, if your page uses them dynamically you should release them explicitly by calling window.URL.revokeObjectURL():

这个对象URL是一个标识File对象的字符串。每次你调用window.URL.createObjectURL(),就会产生一个唯一的对象URL,即使是你对一个已创建了对象URL的文件再次创建一个对象URL。每个创建了的对象URL必须要释放。当文档关闭时,它们会自动被释放。如果你的网页要动态使用它们,你需要显式调用 window.URL.revokeObjectURL()来释放它们:


Example: Using object URLs to display images


This example uses object URLs to display image thumbnails. In addition, it displays other file information including their names and sizes.


The HTML that presents the interface looks like this:


<input type="file" id="fileElem" multiple accept="image/*" style="display:none" onchange="handleFiles(this.files)">
<a href="#" id="fileSelect">Select some files</a> 
<div id="fileList">
  <p>No files selected!</p>

This establishes our file <input> element as well as a link that invokes the file picker (since we keep the file input hidden to prevent that less-than-attractive user interface from being displayed). This is explained in the section Using hidden file input elements using the click() method, as is the method that invokes the file picker.

这确定我们的文件 <input> 元素显示为一个可以调用文件选择器的链接(我们隐藏了文件输入元素来阻止显示用户不友好的界面)。这个在 Using hidden file input elements using the click() method节已经说明了这种调用文件选择器的方法。

The handleFiles() method follows:

handleFiles() 方法如下:

window.URL = window.URL || window.webkitURL;

var fileSelect = document.getElementById("fileSelect"),
    fileElem = document.getElementById("fileElem"),
    fileList = document.getElementById("fileList");

fileSelect.addEventListener("click", function (e) {
  if (fileElem) {;
  e.preventDefault(); // prevent navigation to "#"
}, false);

function handleFiles(files) {
  if (!files.length) {
    fileList.innerHTML = "<p>No files selected!</p>";
  } else {
    fileList.innerHTML = "";
    var list = document.createElement("ul");
    for (var i = 0; i < files.length; i++) {
      var li = document.createElement("li");
      var img = document.createElement("img");
      img.src = window.URL.createObjectURL(files[i]);
      img.height = 60;
      img.onload = function() {
      var info = document.createElement("span");
      info.innerHTML = files[i].name + ": " + files[i].size + " bytes";

This starts by fetching the URL of the <div> with the ID fileList. This is the block into which we'll insert our file list, including thumbnails.

从ID为fileList<div> 开始的这个区块里我们会插入我们的文件列表,包括缩略图。

If the FileList object passed to handleFiles() is null, we simply set the inner HTML of the block to display "No files selected!". Otherwise, we start building our file list, as follows:

如果传递到handleFiles()中的 FileList对象值为 null时,我们只要简单将这块的内部HTML为显示“No files selected!”。否则,我们就需要编写我们的文件列表,如下:

  1. A new unordered list (<ul>) element is created.
  2. The new list element is inserted into the <div> block by calling its Node.appendChild() method.
  3. For each File in the FileList represented by files:
    1. Create a new list item (<li>) element and insert it into the list.
    2. Create a new image (<img>) element.
    3. Set the image's source to a new object URL representing the file, using window.URL.createObjectURL() to create the blob URL.
    4. Set the image's height to 60 pixels.
    5. Set up the image's load event handler to release the object URL since it's no longer needed once the image has been loaded. This is done by calling the window.URL.revokeObjectURL() method and passing in the object URL string as specified by img.src.
    6. Append the new list item to the list.
  1. 创建一个无序列表 (<ul>) 元素。
  2. 通过调用列表的Node.appendChild()方法来将新的列表元素插入到 <div>块。
  3. 遍历文件集合FileList中的每个 File
    1. 创建一个新的列表项 (<li>)元素并插入到列表中。
    2. 创建一个新的图片 (<img>) 元素。
    3. 设置图片的源为一个新的指代文件的对象URL,使用window.URL.createObjectURL()来创建blob URL。
    4. 设置图片的高度为60像素。
    5. 设置图片的load事件处理器来释放对象URL,当图片加载完成之后对象URL就不再需要了。这个可以通过调用window.URL.revokeObjectURL()方法并且传递 img.src中的对象URL字符串来实现。 
    6. 将新的列表项添加到列表中。

Here is a live demo of the code above:


Example: Uploading a user-selected file

Another thing you might want to do is let the user upload the selected file or files (such as the images selected using the previous example) to a server. This can be done asynchronously very easily.

Creating the upload tasks

Continuing with the code that built the thumbnails in the previous example, recall that every thumbnail image is in the CSS class obj with the corresponding File attached in a file attribute. This allows us to select all of the images the user has chosen for uploading using Document.querySelectorAll(), like this:

function sendFiles() {
  var imgs = document.querySelectorAll(".obj");
  for (var i = 0; i < imgs.length; i++) {
    new FileUpload(imgs[i], imgs[i].file);

Line 2 fetches a NodeList, called imgs, of all the elements in the document with the CSS class obj. In our case, these will be all of the image thumbnails. Once we have that list, it's trivial to go through it and create a new FileUpload instance for each. Each of these handles uploading the corresponding file.

Handling the upload process for a file

The FileUpload function accepts two inputs: an image element and a file from which to read the image data.

function FileUpload(img, file) {
  var reader = new FileReader();  
  this.ctrl = createThrobber(img);
  var xhr = new XMLHttpRequest();
  this.xhr = xhr;
  var self = this;
  this.xhr.upload.addEventListener("progress", function(e) {
        if (e.lengthComputable) {
          var percentage = Math.round((e.loaded * 100) /;
      }, false);
  xhr.upload.addEventListener("load", function(e){
          var canvas = self.ctrl.ctx.canvas;
      }, false);"POST", "");
  xhr.overrideMimeType('text/plain; charset=x-user-defined-binary');
  reader.onload = function(evt) {

The FileUpload() function shown above creates a throbber, which is used to display progress information, and then creates an XMLHttpRequest to handle uploading the data.

Before actually transferring the data, several preparatory steps are taken:

  1. The XMLHttpRequest's upload progress listener is set to update the throbber with new percentage information so that as the upload progresses the throbber will be updated based on the latest information.
  2. The XMLHttpRequest's upload load event handler is set to update the throbber progress information to 100% to ensure the progress indicator actually reaches 100% (in case of granularity quirks during the process). It then removes the throbber since it's no longer needed. This causes the throbber to disappear once the upload is complete.
  3. The request to upload the image file is opened by calling XMLHttpRequest's open() method to start generating a POST request.
  4. The MIME type for the upload is set by calling the XMLHttpRequest function overrideMimeType(). In this case, we're using a generic MIME type; you may or may not need to set the MIME type at all depending on your use case.
  5. The FileReader object is used to convert the file to a binary string.
  6. Finally, when the content is loaded the XMLHttpRequest function send() is called to upload the file's content.
Note: The non-standard sendAsBinary method which was previously used in the example above is considered deprecated as of Gecko 31 (Firefox 31 / Thunderbird 31 / SeaMonkey 2.28); use the standard send(Blob data) method instead.

Handling the upload process for a file, asynchronously

if (isset($_FILES['myFile'])) {
    // Example:
    move_uploaded_file($_FILES['myFile']['tmp_name'], "uploads/" . $_FILES['myFile']['name']);
?><!DOCTYPE html>
    <title>dnd binary upload</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script type="text/javascript">
        function sendFile(file) {
            var uri = "/index.php";
            var xhr = new XMLHttpRequest();
            var fd = new FormData();
  "POST", uri, true);
            xhr.onreadystatechange = function() {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    alert(xhr.responseText); // handle response.
            fd.append('myFile', file);
            // Initiate a multipart/form-data upload

        window.onload = function() {
            var dropzone = document.getElementById("dropzone");
            dropzone.ondragover = dropzone.ondragenter = function(event) {
            dropzone.ondrop = function(event) {

                var filesArray = event.dataTransfer.files;
                for (var i=0; i<filesArray.length; i++) {
        <div id="dropzone" style="margin:30px; width:500px; height:300px; border:1px dotted grey;">Drag & drop your file here...</div>

Example: Using object URLs to display PDF

Object URLs can be used for other things than just images! They can be used to display embedded PDF files or any other resources that can be displayed by the browser.

In Firefox, to have the PDF appear embedded in the iframe (rather than proposed as a downloaded file), the preference pdfjs.disabled must be set to false .

<iframe id="viewer">

And here is the change of the src attribute:

var obj_url = window.URL.createObjectURL(blob);
var iframe = document.getElementById('viewer');
iframe.setAttribute('src', obj_url);

Example: Using object URLs with other file types

You can manipulate files of other formats the same way. Here is how to preview uploaded video:

var video = document.getElementById('video');
var obj_url = window.URL.createObjectURL(blob);
video.src = obj_url;


See also


此页面的贡献者: zxsunrise, codevvvv9, chrisdavidmills, soraly, QizhongFang
最后编辑者: zxsunrise,