Javascript 对于axios和ajax文件上载,.NET核心Web API中的IFormFile为空,但在Postman中有效 介绍
因此,我有一个API,它将使用以下签名将文件上载到Azure:Javascript 对于axios和ajax文件上载,.NET核心Web API中的IFormFile为空,但在Postman中有效 介绍,javascript,reactjs,asp.net-core,axios,asp.net-core-webapi,Javascript,Reactjs,Asp.net Core,Axios,Asp.net Core Webapi,因此,我有一个API,它将使用以下签名将文件上载到Azure: public async Task<IActionResult> Post([FromForm]IEnumerable<IFormFile> files) .NETCore通过axios/ajax获得的API请求 NET Core通过Postman获得的API请求 您会注意到这里的主要区别是邮递员在文件下有文件,而axios请求似乎只在结果视图下引用它,而在文件下没有任何内容。我是否在表单中添加了正确的项
public async Task<IActionResult> Post([FromForm]IEnumerable<IFormFile> files)
.NETCore通过axios/ajax获得的API请求
NET Core通过Postman获得的API请求
您会注意到这里的主要区别是邮递员在文件下有文件,而axios请求似乎只在结果视图下引用它,而在文件下没有任何内容。我是否在表单中添加了正确的项目?我已尝试使用以下输入字段中的导入图像和动态图像执行此操作:
<FormGroup id = 'file-form'>
<Label for="file">Attach Files:</Label>
<Input type="file" name="files" id="File" onChange={this.handleFileChange}/>
</FormGroup>
但是没有运气。任何帮助都是极其重要的
谢谢
更新1
在查看了一些Microsoft文档后,我发现Microsoft文档中有一条警告,说明将表单类型转换为多部分/表单数据。我试图这样做,但没有成功。这是我在操作方法中所做的:
[HttpPost]
public void Post([FromForm]ValueFile files)
{
//do logic
}
然后我创建了一个名为ValueFile的ViewModel
public class ValueFile
{
public IEnumerable<IFormFile> Files { get; set; }
}
在我的web客户端中,我使用了jquery ajax:
$('#fileTest').on('change',
function() {
var fd = new FormData();
fd.append('files', $('#fileTest')[0].files[0]);
$.ajax({
"async": true,
"crossDomain": true,
"url": "http://localhost:5000/api/values",
"method": "POST",
"processData": false,
"contentType": false,
"mimeType": "multipart/form-data",
"data": fd
}).done(function (response) {
console.log(response);
});
});
<form id='file-form'>
<Label for="file">Attach Files:</Label>
<Input type="file" name="files" id="fileTest" />
</form>
希望这有帮助这是我在“行动方法”中所做的:
[HttpPost]
public void Post([FromForm]ValueFile files)
{
//do logic
}
然后我创建了一个名为ValueFile的ViewModel
public class ValueFile
{
public IEnumerable<IFormFile> Files { get; set; }
}
在我的web客户端中,我使用了jquery ajax:
$('#fileTest').on('change',
function() {
var fd = new FormData();
fd.append('files', $('#fileTest')[0].files[0]);
$.ajax({
"async": true,
"crossDomain": true,
"url": "http://localhost:5000/api/values",
"method": "POST",
"processData": false,
"contentType": false,
"mimeType": "multipart/form-data",
"data": fd
}).done(function (response) {
console.log(response);
});
});
<form id='file-form'>
<Label for="file">Attach Files:</Label>
<Input type="file" name="files" id="fileTest" />
</form>
希望这有帮助你这个该死的天才,我不知道为什么将文件表单分离到它自己的类中解决了这个问题,但是现在它可以上传了。你这个该死的天才,我不知道为什么将文件表单分离到它自己的类中解决了这个问题,但是现在它可以上传了。