![](/img/trans.png)
[英]When trying to upload file, FileBufferingReadStream.ThrowIfDisposed() is thrown
[英]When trying to upload file, FileBufferingReadStream.ThrowIfDisposed() error occurr
我在控制器中收到一个具有Microsoft.AspNetCore.Http.IFormFile
类型的图像文件。
然后将此文件上传到Azure Blob。
在此之前,我采取以下步骤
控制者
[HttpPost]
public async void ActionMethod(IFormFile img)
{
// some process
using(MemoryStream stream = new MemoryStream())
{
// (1)
img.CopyTo(stream); // (2)
stream.Seek(0, SeekOrigin.Begin);
// call await cloud block blob.UploadFromStreamAsync(stream);
}
// some process
}
当using
流通过时
流
CanRead: true
CanSeek: true
CanTimeout: false
CanWrite: true
Capacity: 0
Length: 0
Position: 0
ReadTimeout: 'stream.ReadTimeout' threw an exception of type 'System.InvalidOperationException'
WriteTimeout: 'stream.ReadTimeout' threw an exception of type 'System.InvalidOperationException'
在(2)
点及之后出现以下错误
System.ObjectDisposedException occurred
HResult=0x80131622
Message=Cannot access a disposed object
ObjectName: 'FileBufferingReadStream'
Source=Microsoft.AspNetCore.WebUtilities
StackTrace:
at Microsoft.AspNetCore.WebUtilities.FileBufferingReadStream.ThrowIfDisposed()
at Microsoft.AspNetCore.WebUtilities.FileBufferingReadStream.set_Position(Int64 value)
at Microsoft.AspNetCore.Http.Internal.ReferenceReadStream..ctor(Stream inner, Int64 offset, Int64 length)
at Microsoft.AspNetCore.Http.Internal.FormFile.OpenReadStream()
at Microsoft.AspNetCore.Http.Internal.FormFile.CopyTo(Stream target)
at AzureStorageManager.AzureStorageFileModules.<UploadFileAsync>d__11.MoveNext()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
真正的问题是,obove代码在某些情况下可以正常工作,但在某些情况下却无法工作(我无法抓住这种情况。每次file
都相同)。
请帮我...
我花了一天的时间来解决这个问题,在上传了这个问题之后,我立即找到了解决方案。 导致此问题的问题是Action方法的返回类型。 如果返回类型不是Task<T>
,则会发生错误。 所以我固定了我的动作方法
[HttpPost]
public async Task<int> ActionMethod(IFormFile img)
{
// same
return resultValue;
}
之后,错误不会显示。 其实我不知道为什么。 因此,如果您知道,请告诉我并分享您的知识 。 谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.