如何在 JavaScript 中实例化 File 对象? [英] How to instantiate a File object in JavaScript?
本文介绍了如何在 JavaScript 中实例化 File 对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
JavaScript 中有一个 File
对象.我想实例化一个用于测试目的.
There's a File
object in JavaScript. I want to instantiate one for testing purposes.
我已经尝试过 new File()
,但我收到了非法构造函数"错误.
I have tried new File()
, but I get an "Illegal constructor" error.
是否可以创建一个 File
对象?
Is it possible to create a File
object ?
文件对象参考:https://developer.mozilla.org/en/DOM/文件
推荐答案
根据 W3C File API 规范,File 构造函数需要 2 个(或 3 个)参数.
According to the W3C File API specification, the File constructor requires 2 (or 3) parameters.
所以要创建一个空文件:
So to create a empty file do:
var f = new File([""], "filename");
- 第一个参数是作为文本行数组提供的数据;
- 第二个参数是文件名;
第三个参数看起来像:
- The first argument is the data provided as an array of lines of text;
- The second argument is the filename ;
The third argument looks like:
var f = new File([""], "filename.txt", {type: "text/plain", lastModified: date})
它适用于 FireFox、Chrome 和 Opera,但不适用于 Safari 或 IE/Edge.
It works in FireFox, Chrome and Opera, but not in Safari or IE/Edge.
这篇关于如何在 JavaScript 中实例化 File 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文