【问题标题】:FormData append is an empty object after appending a fileFormData append 是追加文件后的空对象
【发布时间】:2019-01-17 18:01:29
【问题描述】:

这是我尝试附加并发送到服务器的文件(console.log):

...
console.log(myFile); // brings this:

File(1921512) {name: "2018-10-07_23-53-14.png", lastModified: 1538945346813, lastModifiedDate: Sun Oct 07 2018 23:54:06 GMT+0300 (Israel Daylight Time), webkitRelativePath: "", size: 1921512, …}
lastModified: 1538945346813
lastModifiedDate: Sun Oct 07 2018 23:54:06 GMT+0300 (Israel Daylight Time) {}
name: "2018-10-07_23-53-14.png"
size: 1921512
type: "image/png"
webkitRelativePath: ""
__proto__: File

我像这样附加这个文件:

let formData = new FormData();
formData.append(0, myFile);

然后我做:

console.log(formData); // FormData {}__proto__: FormData

那里没有附加文件。

当我尝试将其发送到服务器时,Http 请求标头的内容类型不包含任何内容类型:

请求标头:

Accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: he-IL,he;q=0.9,en-US;q=0.8,en;q=0.7,pt;q=0.6,la;q=0.5
Access-Control-Request-Headers: authorization
Access-Control-Request-Method: POST
Cache-Control: no-cache
Connection: keep-alive
Host: localhost:3000
Origin: http://localhost:8080
Pragma: no-cache
Referer: http://localhost:8080/
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36

我已尝试使用实体记录此内容,但这仍然无法正常工作 - 无论如何服务器没有获取任何文件。

【问题讨论】:

  • 你试过了吗:formData.append("my-file", new Blob([myFile], { type: "image/png" }));

标签: javascript ajax xmlhttprequest multipartform-data


【解决方案1】:

我在请求参数中添加了:transformRequest: angular.identity

【讨论】:

    猜你喜欢
    • 2017-02-09
    • 1970-01-01
    • 2017-10-17
    • 2017-12-06
    • 1970-01-01
    • 2015-08-03
    • 2016-08-17
    • 2017-06-28
    • 2015-09-04
    相关资源
    最近更新 更多