【问题标题】:Uploading multiple images does not get added to FormData上传多个图像不会添加到 FormData
【发布时间】:2019-04-20 15:36:01
【问题描述】:

我有检测文件的方法(在本例中为图像):

detectFiles(event) {
this.formData = new FormData();
this.urls = [];
this.files = event.target.files;
if (this.files) {
  for (const file of this.files) {
    const reader = new FileReader();
    reader.onload = (e: any) => {
      this.urls.push(e.target.result);
    };
    reader.readAsDataURL(file);
  }
}}

我得到的文件保存在this.files 并在提交按钮上我这样做:

  submitForm(value: any) {
if (value) {
  this.formData.append('Title', value.title);
  this.formData.append('Date', value.date);

  for (let i = 0; i < this.files.length; i++) {
    const chosenFileName = this.files[i].name;
    const chosenFile = this.files[i];
    this.formData.append('file', chosenFileName, chosenFile);
  }

  this.authService.uploadFile(this.formData)
    .subscribe(
      (response) => {

      },
      (error) => {
      }
    );
}}

在这里,我从输入中添加值,然后通过循环添加我拥有的所有文件。 在示例中,我添加了图片,但它们没有出现在请求中。

我在这里做错了什么?

【问题讨论】:

    标签: javascript angular typescript form-data


    【解决方案1】:

    这个错误是假的:

    for (let i = 0; i < this.files.length; i++) {
        const chosenFileName = this.files[i].name;debugger;
        const chosenFile = this.files[i];
        this.formData.append('file', chosenFile); // <----- changed this line
      }
    

    【讨论】:

      猜你喜欢
      • 2019-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-17
      • 2020-02-14
      • 2017-10-24
      • 2018-02-20
      • 2019-05-04
      相关资源
      最近更新 更多