简体   繁体   English

Angular POST multipart/form-data with application/json & text/csv

[英]Angular POST multipart/form-data with application/json & text/csv

PROBLEM问题

Trying to send a multipart/formdata post request with a file upload and model data in JSON.尝试使用 JSON 格式的文件上传和模型数据发送 multipart/formdata post 请求。 Not sure why there are not much working references, couldn't make work the ones which are there.不知道为什么没有太多的工作参考,无法使那些存在的工作。 Might be doing something wrong as well or making it more complicated too.也可能做错了什么,或者让它变得更复杂。

REFS TRIED参考文献

QUICK FIDDLE WITH MY TRIES快速调整我的尝试

QUESTION

Somehow json content on my post request payload doesn't seem to contain Content-Type: application/json identified automatically.不知何故,我的帖子请求有效负载上的 json 内容似乎不包含自动识别的 Content-Type: application/json。

Even setting the content-type as undefined/false for the POST didnt work.即使将 POST 的内容类型设置为 undefined/false 也不起作用。 Need some suggestion on what would be the problem or how to fix this?需要一些关于问题或如何解决的建议?

NETWORK LOG FOR FIDDLE UPLOAD小提琴上传的网络日志

Request Headers请求头

Accept:application/json, text/plain, */*
Content-Length:361
Content-Type:multipart/form-data; boundary=----WebKitFormBoundary7NukqN6GtRwugBSJ

Request Payload请求有效载荷

------WebKitFormBoundary7NukqN6GtRwugBSJ
Content-Disposition: form-data; name="file"; filename="sample.csv"
Content-Type: text/csv


------WebKitFormBoundary7NukqN6GtRwugBSJ
Content-Disposition: form-data; name="data"

{"name":"a good name","comments":"some comments"}
------WebKitFormBoundary7NukqN6GtRwugBSJ--

Faced the same issue.Try using blob and You can set the type in the blob as application/json.面临同样的问题。尝试使用 blob,您可以将 blob 中的类型设置为 application/json。 You can initialize the blob with your values and it worked for me.您可以使用您的值初始化 blob,它对我有用。

If you make a "multipart/form-data" request, then automatically, the "application/JSON" header will be left out. 如果您发出“multipart / form-data”请求,那么将自动删除“application / JSON”标题。 You cannot have both in the same request. 您不能同时拥有这两个请求。 The two post requests are mutually exclusive. 这两个帖子请求是互斥的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM