文件上传 asp.net 核心 - System.ObjectDisposedException: 'Cannot access a closed file.'
File upload asp.net core - System.ObjectDisposedException: 'Cannot access a closed file.'
我正在使用 asp.net 核心和 C# 编程。
我在控制器中有一个方法可以从视图中的表单上传文件。
public async void UploadFile([FromForm(Name = "aFile")] IformFile aFile)
{
var filePath = Path.Combine(_webhost.WebRootPath, "Images", aFile.FileName);
if (aFile.Length > 0)
{
using (FileStream fs = System.IO.File.Create(filePath))
{
await aFile.CopyToAsync(fs);
}
}
}
A 155kb上传成功;然而,一个 3.38 MB 的文件失败了:
System.ObjectDisposedException: 'Cannot access a closed file.'
我了解到该问题可能与限制和流处理有关;然而,尽管添加了我在堆栈溢出方面推荐的修复程序,但问题仍然存在,例如:
[HttpPost, DisableRequestSizeLimit, RequestFormLimits(MultipartBodyLengthLimit = Int32.MaxValue, ValueLengthLimit = Int32.MaxValue, ValueCountLimit = Int32.MaxValue)]
欢迎任何建议:)谢谢!
此线程中记录了可能的修复:https://forums.asp.net/t/1397944.aspx?+Cannot+access+a+closed+file。
具体来说,在您的网络配置中更改 'requestLengthDiskThreshold' 的值。
<system.web>
<httpRuntime executionTimeout="90" maxRequestLength="20000" useFullyQualifiedRedirectUrl="false" requestLengthDiskThreshold="8192"/>
</system.web>
在@System.ObjectDisposedException: Cannot access a closed Stream
中提到了类似的修复
由于使用 async void
,您的 运行 进入了竞争状态。由于 void
return 类型,服务器代码 运行 您的应用无法知道您的方法何时完成并立即处理请求。
使用 Task
作为 UploadFile
方法的 return 类型。
在使用 async
.
时,通常应避免使用 void
return 类型
See the article from Microsoft for more infos
我正在使用 asp.net 核心和 C# 编程。
我在控制器中有一个方法可以从视图中的表单上传文件。
public async void UploadFile([FromForm(Name = "aFile")] IformFile aFile)
{
var filePath = Path.Combine(_webhost.WebRootPath, "Images", aFile.FileName);
if (aFile.Length > 0)
{
using (FileStream fs = System.IO.File.Create(filePath))
{
await aFile.CopyToAsync(fs);
}
}
}
A 155kb上传成功;然而,一个 3.38 MB 的文件失败了:
System.ObjectDisposedException: 'Cannot access a closed file.'
我了解到该问题可能与限制和流处理有关;然而,尽管添加了我在堆栈溢出方面推荐的修复程序,但问题仍然存在,例如:
[HttpPost, DisableRequestSizeLimit, RequestFormLimits(MultipartBodyLengthLimit = Int32.MaxValue, ValueLengthLimit = Int32.MaxValue, ValueCountLimit = Int32.MaxValue)]
欢迎任何建议:)谢谢!
此线程中记录了可能的修复:https://forums.asp.net/t/1397944.aspx?+Cannot+access+a+closed+file。
具体来说,在您的网络配置中更改 'requestLengthDiskThreshold' 的值。
<system.web>
<httpRuntime executionTimeout="90" maxRequestLength="20000" useFullyQualifiedRedirectUrl="false" requestLengthDiskThreshold="8192"/>
</system.web>
在@System.ObjectDisposedException: Cannot access a closed Stream
中提到了类似的修复由于使用 async void
,您的 运行 进入了竞争状态。由于 void
return 类型,服务器代码 运行 您的应用无法知道您的方法何时完成并立即处理请求。
使用 Task
作为 UploadFile
方法的 return 类型。
在使用 async
.
时,通常应避免使用 void
return 类型
See the article from Microsoft for more infos