I have this client code on web-client to load some kind of file.
fileUploaded(files) {
files.forEach(async ifile => {
await axios.post('http://localhost:5000/api/upload', ifile)
.then(res => {
console.log(res)
}).catch(err => {
console.error(err);
});
});
}
My endpoint code looks like this:
[HttpPost("upload")]
public async Task<IActionResult> UploadFile([FromBody] IFormFile file)
{
var data = new byte[file.Length];
using (var bstream = file.OpenReadStream())
{
while (bstream.CanRead)
{
bstream.Read(data);
}
}
// etc
return Ok();
}
Parameter file
is always null. How to deliver this file from the client the right way?
Instead of
UploadFile([FromBody] IFormFile file)
Use
UploadFile([FromForm] IFormFile file)
And
fileUploaded(files) {
files.forEach(async ifile => {
const formData = new FormData();
formData.append('file', ifile)
await axios.post('http://localhost:5000/api/upload', formData)
.then(res => {
console.log(res)
}).catch(err => {
console.error(err);
});
});
}
in the UploadFile method, just, do this
//if asp.net mvc
var file1 = System.Web.HttpContext.Current.Request.Files[0];
//if asp.net core
var file2 = Request.Form.Files[0];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.