代码之家  ›  专栏  ›  技术社区  ›  user9487981

无法读取输入到表单typescript angular中的文件的type属性

  •  0
  • user9487981  · 技术社区  · 8 年前

    我有一个正在上传的文件表单。

    samplemenuupload(event){
        let submittedpdfs = event.target.files;
        for(let pdf in submittedpdfs){
          console.log(submittedpdfs[+pdf]);
          if(submittedpdfs[+pdf].type !== acceptedpdf){
            submittedpdfs.splice(+pdf,1);
          }
        }
    

    文件对象如下所示。正如你所看到的,它有一个 type 所有物

    File(127001) {name: "2017-02-23-statements-1469.pdf", lastModified: 1492974543668, lastModifiedDate: Sun Apr 23 2017 14:09:03 GMT-0500 (CDT), webkitRelativePath: "", size: 127001, …}
    lastModified:1492974543668
    lastModifiedDate:Sun Apr 23 2017 14:09:03 GMT-0500 (CDT) {}
    name:"2017-02-23-statements-1469.pdf"
    size:127001
    type:"application/pdf"
    webkitRelativePath:""
    

    当我检查if语句中的类型时,会出现以下错误:

    ERROR TypeError: Cannot read property 'type' of undefined
    

    怎么了?我做错什么了?

    1 回复  |  直到 8 年前
        1
  •  0
  •   Sajeetharan    8 年前

    尝试分配对象,然后访问,

    let data = submittedpdfs[+pdf]);
    if(data.type !== acceptedpdf)