如何在JavaScript中实例化一个File对象? [英] How to instantiate a File object in JavaScript?

查看:928
本文介绍了如何在JavaScript中实例化一个File对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文件 在JavaScript中的对象。我想实例化一个用于测试的目的。

我试过 new File(),但是我得到一个非法的构造函数错误。

是否可以创建一个文件对象?






文件对象参考: https://developer.mozilla.org/en/DOM/File

根据W3C File API规范,File构造函数需要2个(或3个)参数。

所以要创建一个空文件吗:

  var f = new File([],filename); 




  • 第一个参数是以文本行数组的形式提供的数据;
  • 第二个参数是文件名;
  • 第三个参数是:

      var f = new File([],filename.txt,{type:text / plain,lastModified:date})
    <它可以在FireFox,Chrome和Opera中使用,但不能在Safari或IE浏览器中使用。 / Edge。

    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对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆