【发布时间】:2018-05-21 14:27:27
【问题描述】:
我正在使用 React 和 Axios 将 formData 发布到内部 .NET API。
API 需要这样的数据:
[HttpPost("upload")]
public virtual async Task<IActionResult> PostMulti(Int64 parentId, ICollection<IFormFile> fileData)
{
foreach (var file in fileData) {
await SaveFile(file, parent);
}
return Created("", Map(Repository.Get(parentId)));
}
当我单步调试时,“fileData”的计数始终为 0。
这是我使用 Axios 发送它的方式:
const formData = new FormData();
formData.append('image', this.state.file);
console.log("this.state.file = ", this.state.file);
console.log("formData = ", formData);
axios({
url: `/api/gameMethods/playerStates/${this.props.playerId}/files/upload`,
method: 'POST',
data: formData,
headers: {
Accept: 'application/json',
'Content-Type': 'multipart/form-data'
}
})
.then((response) => {
//handle success
console.log('response -- then: ', response);
this.setState({
file: this.state.file
});
})
.catch((response) => {
//handle error
console.log('response -- catch: ', response);
});
我使用 console.log 进行调试。当我写出文件对象(名称、大小等)时,它会向我显示文件对象。
它还在方法的“.then”处理程序中触发并显示:
“响应——则:数据:数组(0),状态:201,状态文本:“已创建” "
所以,我不知道为什么它没有向 API 发送任何内容,我真的不知道发生了什么或如何解决这个问题。
任何帮助将不胜感激。
谢谢!
【问题讨论】:
标签: javascript .net asp.net-mvc reactjs axios