我正在尝试使用IFormFile
上传图像。但是我在这一行出现了错误:
using (var stream = _file.OpenReadStream())
错误(stacktrace在底部):
ObjectDisposedException:无法访问已处置的对象。对象名称: 'FileBufferingReadStream'。 Microsoft.AspNetCore.WebUtilities.FileBufferingReadStream.ThrowIfDisposed()
我已经在寻找解决方案,但是没有一个对我有用。
这是该方法的完整代码:
public async Task<IActionResult> Create(Product product)
{
var newFileName = string.Empty;
var fileName = string.Empty;
if (ModelState.IsValid)
{
IFormFile _file = UploadFiles.File;
ResizeOptions resize;
var myUniqueFileName = Convert.ToString(Guid.NewGuid());
var FileExtension = Path.GetExtension(_file.FileName);
newFileName = myUniqueFileName + FileExtension;
fileName = Path.Combine(_env.WebRootPath, "ProductImages") + $@"\{newFileName}";
using (var stream = _file.OpenReadStream())
{
using (var output = new MemoryStream())
using (Image image = Image.Load(stream))
{
image.Mutate(x => x.Resize(image.Width / 3, image.Height));
image.Save(Path.Combine(_env.WebRootPath, @"ProductImages\Thumbnails") + $@"\{newFileName}");
}
}
}
}
StackTrace:
Microsoft.AspNetCore.WebUtilities.FileBufferingReadStream.ThrowIfDisposed()
Microsoft.AspNetCore.WebUtilities.FileBufferingReadStream.set_Position(long value)
Microsoft.AspNetCore.Http.Internal.FormFile.OpenReadStream()
netcore.Controllers.Invent.ProductController+<Create>d__7.MoveNext() in ProductController.cs
+
using (var stream = _file.OpenReadStream())
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker+<InvokeActionMethodAsync>d__12.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker+<InvokeNextActionFilterAsync>d__10.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker+<InvokeInnerFilterAsync>d__14.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+<InvokeNextResourceFilter>d__22.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+<InvokeFilterPipelineAsync>d__17.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+<InvokeAsync>d__15.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Builder.RouterMiddleware+<Invoke>d__4.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Authentication.AuthenticationMiddleware+<Invoke>d__6.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.MigrationsEndPointMiddleware+<Invoke>d__4.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware+<Invoke>d__6.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware+<Invoke>d__6.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware+<Invoke>d__7.MoveNext()
谢谢
答案 0 :(得分:0)
我创建了另一个解决方案,它可以工作。我不知道为什么:D