Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

File.getAsText

概述

按照指定的编码类型将文件内容解析成字符串并返回.

示例

// fileInput是一个HTMLInputElement元素: <input type="file" id="myfileinput" multiple>
var fileInput = document.getElementById("myfileinput");

// files是一个FileList对象(类似NodeList)
var files = fileInput.files;

// 一个对象,包含了允许的MIME类型
var accept = {
    binary : ["image/png", "image/jpeg"],
    text :   ["text/plain", "text/css", "application/xml", "text/html"]
};

var file;

for (var i = 0; i < files.length; i++) {

    file = files[i];

    // 如果文件的类型能够被检测到
   if (file !== null) {

        if (accept.binary.indexOf(file.mediaType) > -1) {

            // file是个可接受的文本文件,使用utf-8编码读取
            var data = file.getAsText("utf-8");
            // 使用字符串方法处理data

        } else if (accept.binary.indexOf(file.mediaType) > -1) {
            // file是个可接受的二进制文件
        }
    }
}

文档标签和贡献者

 此页面的贡献者: teoli, ziyunfei
 最后编辑者: teoli,