我被困在这里了,我试图同时从我的indexedDb发布一个json对象和一个IFormFile对象到服务器。接受它的方法如下所示:
[HttpPost]
public async Task<IActionResult> Create(BatchModel model, IFormFile vinFile)
{
//logic goes here
}在我不得不将我的batchModels存储为Json对象之前,这个方法在早期是有效的,并且是通过POST直接从表单发布的。从那以后发生了很大的变化,现在客户端一从离线状态重新上线就会上传它,方法如下(简化):
if (infoChanged === true) {
fetchPromises.push(
fetch('/Batch/Create/', {
headers: new Headers({
'Content-Type': 'application/javascript' //Tried this with multi-part/form
}),
credentials: 'same-origin',
method: 'POST',
body: batch //, vinFile
})
);
}
return Promise.all(fetchPromises);为了测试,我尝试使用只填充模型的方法,我唯一需要更改的是,我必须在C#代码中更改添加FromBody标记,但现在我也需要填充vinFile。我已经用一个FormData对象进行了尝试,将批处理和vinFile附加到与Create函数中相同的名称。但这会导致两个变量都为空。
发布于 2017-12-27 21:26:05
你需要看的关键特性是你的头。如果执行header:{ Content-Type:"application/ JSON“},则可以将响应更改为json
你想要这样的东西:你需要自己配置类型。
fetch(myRequest).then(function(response) {
var contentType = response.headers.get("content-type");
if(contentType && contentType.includes("application/json")) {
return response.json();
}
throw new TypeError("Oops, we haven't got JSON!");
})
.then(function(json) { /* process your JSON further */ })
.catch(function(error) { console.log(error); });https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
https://stackoverflow.com/questions/47992730
复制相似问题