使用 JavaScript 的 FileReader 接口时检测文件的内容类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4264289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 11:38:39  来源:igfitidea点击:

Detecting a file's content-type when using JavaScript's FileReader interface

javascriptimportcontent-typeplaintextfilereader

提问by u365975

I've been setting up an import script for plain-text files in a web application.

我一直在为 Web 应用程序中的纯文本文件设置导入脚本。

My script is as follows:

我的脚本如下:

function dataImport(files) {
    confirm("Are you sure you want to import the selected file? This will overwrite any data that is currently saved in the application workspace.");
    for (i = 0; i < files.length; i++) {
        file = files[i]
        console.log(file)
        var reader = new FileReader()
        ret = []
        reader.onload = function(e) {
            window.localStorage.setItem("ApplicationData", e.target.result);
        }
        reader.onerror = function(stuff) {
            console.log("error", stuff)
            console.log (stuff.getMessage())
        }
        reader.readAsText(file)
    }
}

It's essentially a modification of that posed on this question.

它本质上是对这个问题提出的修改。

However, at the moment the user can technically attempt to import any file. As it's designed for plain-text files, problems can arise if a different type of file is imported.

但是,目前用户可以在技术上尝试导入任何文件。由于它是为纯文本文件设计的,如果导入不同类型的文件,可能会出现问题。

I've noticed in the console that the browser detects the content-type of the file being imported. Here's an example.

我在控制台中注意到浏览器检测到正在导入的文件的内容类型。这是一个例子。

fileName: "ideas.txt"
fileSize: 377
name: "ideas.txt"
size: 377
type: "text/plain"
webkitRelativePath: ""

Is it possible, then, to set up an argument where the script detects the content-type of the file, and if it isn't one of a number of specified suitable content-types, have the script refuse to import it?

那么,是否可以设置一个参数,让脚本检测文件的内容类型,如果它不是许多指定的合适内容类型之一,脚本是否拒绝导入它?

Thanks in advance for any advice.

提前感谢您的任何建议。

回答by trembl

if (file.type.match('text/plain')) {
    // file type is text/plain
} else {
    // file type is not text/plain
}

String.match is a RegEx, so if you would want to check, if the file is any type of text, you could do that:

String.match 是一个 RegEx,所以如果你想检查文件是否是任何类型的文本,你可以这样做:

if (file.type.match('text.*')) {
    // file type starts with text
} else {
    // file type does not start with text
}

回答by Benny Neugebauer

The Content Type can be read with the following code:

可以使用以下代码读取内容类型:

// Note: File is a file object than can be read by the HTML5 FileReader API
var reader = new FileReader();

reader.onload = function(event) {
  var dataURL = event.target.result;
  var mimeType = dataURL.split(",")[0].split(":")[1].split(";")[0];
  alert(mimeType);
};

reader.readAsDataURL(file);