【发布时间】:2021-01-14 19:38:44
【问题描述】:
我有一个 .net 5 API,它有一个端点,它接受一个由 IFormFile 文件、字符串描述和字符串名称组成的模型。
当使用我的反应客户端的 axios 发布表单时,使用 55.500kb 文件一切正常,但是当发布 250,000kb 的文件时,请求似乎没有任何问题离开客户端,但当端点为时整个请求为空命中。
我的 API 端点使用 2147483648 的 RequestSizeLimit 进行修饰,并在 web.config 中进行镜像。
我猜原因是发布的文件太大但不能 100% 确定。如果这是我需要以某种方式流式传输或分块请求的原因,还是其他原因导致此问题?
React 函数发布表单:
handleSubmit = async () => {
this.setState({ componentState: LOADING }, async () => {
let form = new FormData();
form.append("title", this.state.title);
form.append("reference", this.state.reference);
form.append("description", this.state.description);
form.append("price", this.state.price);
form.append("scormFile", this.state.file);
let response = await uploadCourseAsync(form);
if (response.isSuccessStatusCode) {
this.setState({ componentState: SUCCESS });
}
else {
this.setState({ errors: response.errors })
}
})
}
.net 端点接收请求:
[RequestSizeLimit(2147483648)]
[HttpPost("UploadCourse")]
public async Task<IActionResult> UploadCourse([FromForm] CreateCourseCommand command)
{
var ms = new MemoryStream();
await command.ScormFile.CopyToAsync(ms);
var byteArrayContent = new ByteArrayContent(ms.ToArray());
var multipartContent = new MultipartFormDataContent
{
{byteArrayContent, command.ScormFile.Name, command.ScormFile.FileName},
{new StringContent(command.Description), "Description"},
{new StringContent(command.Reference), "Reference"},
{new StringContent(command.Price), "Price"},
{new StringContent(command.Title), "Title"},
{new StringContent(User.Claims.FirstOrDefault(c => c.Type == "Id")?.Value ?? string.Empty), "UploadedById"},
{byteArrayContent, command.ScormFile.Name, command.ScormFile.FileName}
};
var url = _serviceUrlConfig.CourseService + "/course/uploadcourse";
var myHttpClient = new HttpClient();
var response = await myHttpClient.PostAsync(url, multipartContent);
return Ok(response);
}
任何建议都会很棒,在此先感谢。
【问题讨论】:
标签: c# reactjs file-upload .net-5