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.getAsBinary

概述

将文件内容按照原始二进制形式解析成字符串并返回.

示例

// 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是个可接受的二进制文件
            var data = file.getAsBinary();
        } else if (accept.binary.indexOf(file.mediaType) > -1) {
            // file是个可接受的文本文件
            var data = file.getAsText();
            // 使用字符串方法处理data
        }
    }
}

文档标签和贡献者

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