【问题标题】:How to submit form which contain <input type="file"/> in Angular 2如何在 Angular 2 中提交包含 <input type="file"/> 的表单
【发布时间】:2016-10-17 10:24:47
【问题描述】:

media.html

<form #form="ngForm" (ngSubmit)="uploadFile(form.value)">
  <input type="file" ngControl="inputFile" />
  <input type="text" ngControl="name"/>
  <button type="submit" >Upload</button>
</form>

media.ts

uploadFile(fileUpload){
    console.log(fileUpload); // this 
    this.mediaService.addMedia(fileUpload).subscribe((r)=> {
        console.log(r);
      },
      (error) => {
        console.log(error.text());
      }
    );
  }

行:console.log("fileUpload"); .它打印出来:

inputFile:null

name: "asdasdasdasd"

这意味着我无法获取 的值以在我的服务器上提交。怎么抓?感谢您的帮助!

【问题讨论】:

标签: angular typescript


【解决方案1】:

你好,为了通过表单标签发送文件,你必须有属性

ENCTYPE = "multipart/form-data"

在表单标签中

这反过来又可以帮助您上传文件

【讨论】:

  • 那么这对提交文件数据有什么帮助呢?这是使用angular 2+,表单无法正常提交。
猜你喜欢
  • 1970-01-01
  • 2012-05-06
  • 2014-02-07
  • 2012-08-07
  • 1970-01-01
  • 2011-09-06
  • 1970-01-01
  • 2015-12-02
  • 1970-01-01
相关资源
最近更新 更多