我是react.js和ASP.Net核心2.0的新手.现在使用ASP.Net core 2.0作为后端API和react.js作为应用程序接口(前端)编写项目.我想知道如何上传文件.我尝试了如下,但在后端方面,参数值(IFromFile文件)始终为null.而且似乎文件没有正确发布.这是我的代码:
.Net核心(API)
[HttpPost]
[Route("upload")]
public async Task Upload(IFormFile file)
{
if (file == null) throw new Exception("File is null");
if (file.Length == 0) throw new Exception("File is empty");
using (Stream stream = file.OpenReadStream())
{
using (var binaryReader = new BinaryReader(stream))
{
var fileContent = binaryReader.ReadBytes((int)file.Length);
// await _uploadService.AddFile(fileContent, file.FileName, file.ContentType);
}
}
}
Run Code Online (Sandbox Code Playgroud)
React.js
handleClick(event){
event.preventDefault();
// console.log("handleClick",event);
var self = this;
var apiBaseUrl = axios.defaults.baseURL + "user/upload";
if(this.state.filesToBeSent.length>0){
var filesArray = this.state.filesToBeSent;
const reader = new FileReader(); …Run Code Online (Sandbox Code Playgroud)