本文介绍了如何在JavaScript中实例化一个File对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有在JavaScript中的对象。我想实例化一个用于测试的目的。
我试过 new File()
,但是我得到一个非法的构造函数错误。
是否可以创建一个文件
对象?
文件对象参考:
有在JavaScript中的对象。我想实例化一个用于测试的目的。
我试过 new File()
,但是我得到一个非法的构造函数错误。
是否可以创建一个文件
对象?
文件对象参考:
根据W3C File API规范,File构造函数需要2个(或3个)参数。
所以要创建一个空文件吗:
var f = new File([],filename);
第三个参数是:
var f = new File([],filename.txt,{type:text / plain,lastModified:date})
There's a File
object in JavaScript. I want to instantiate one for testing purposes.
I have tried new File()
, but I get an "Illegal constructor" error.
Is it possible to create a File
object ?
File Object reference : https://developer.mozilla.org/en/DOM/File
解决方案 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})
It works in FireFox, Chrome and Opera, but not in Safari or IE/Edge.
这篇关于如何在JavaScript中实例化一个File对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!