【问题标题】:Appending data to FormData in angular以角度将数据附加到FormData
【发布时间】:2019-05-13 13:21:27
【问题描述】:

我有一个文件,我想在将其发送到服务器进行上传之前将其属性附加到 formData,但似乎无法附加。这是代码

uploadFile() {
let data = new FormData();
data.append('image', this.file, this.file.name);
console.log(data);
}

即使文件变量具有文件数据,我也会在控制台中获得一个空对象。是不是少了什么?

【问题讨论】:

  • 也许这个答案会对你有所帮助:stackoverflow.com/questions/47936183/angular-file-upload/…
  • 你好,尝试给 console.log(data.get('image')),让我知道它是否有效
  • 这是我得到的:文件(4659) lastModified: 1530958011139 lastModifiedDate: Sat Jul 07 2018 11:06:51 GMT+0100 (West Africa Standard Time) {} name: "17334146_982301731873467_6292546_2173. jpg" 大小: 4659 类型: "image/jpeg" webkitRelativePath: "" proto: File
  • FormData 不是一个简单的对象,所以并不意味着它不是附加的。在 Network DevTools 中检查您发送到服务器的内容。
  • 感谢您提供了很多信息

标签: angular file-upload form-data


【解决方案1】:

试试这个:

<input type="file" (change)="fileService.uploadPhoto(file.files)">

在你的文件服务中:

uploadPhoto(files) {

    if (files.length > 0) {
        const formData = new FormData();
        for (let file of files)
            formData.append(file.name, file);
        .
        .
        .
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-27
    • 2018-03-27
    • 2015-12-23
    • 1970-01-01
    • 2020-12-03
    • 2012-12-11
    相关资源
    最近更新 更多