尝试将 base64String 转换为 IFormFile 在 C# 中引发 SystemInvalidOperation 异常
Trying to convert a base64String into a IFormFile throws SystemInvalidOperation exception in C#
我正在尝试将 base64String 列表转换为 IFormFile 列表。当我尝试 MemoryStream stream = new MemoryStream(bytes)
时,我得到了 stream.ReadTimeout SystemInvalidOperation
。那么为什么它不起作用?
这是我的代码:
private async Task<List<IFormFile>> Base64ToImage(List<EquipmentFile> equipmentFiles)
{
List<IFormFile> formFiles = new List<IFormFile>();
foreach (var eqp in equipmentFiles)
{
byte[] bytes = Convert.FromBase64String(eqp.File);
MemoryStream stream = new MemoryStream(bytes);
IFormFile file = new FormFile(stream, 0, bytes.Length, eqp.Name, eqp.Name);
formFiles.Add(file);
}
return formFiles;
}
我的堆栈跟踪:
System.InvalidOperationException: Timeouts are not supported on this stream.
at System.IO.Stream.get_ReadTimeout()
at FieldService.CallCenter.Api.Controllers.EquipmentLibraryController.Base64ToImage(List`1 equipmentFiles) in C:\Users\CMTech-DEV\Documents\cmtech\fieldservice-api\src\CallCenter\FieldService.CallCenter.API\Controllers\EquipmentLibraryController.cs:line 331
at System.IO.Stream.get_ReadTimeout()
at FieldService.CallCenter.Api.Controllers.EquipmentLibraryController.Base64ToImage(List`1 equipmentFiles) in C:\Users\CMTech-DEV\Documents\cmtech\fieldservice-api\src\CallCenter\FieldService.CallCenter.API\Controllers\EquipmentLibraryController.cs:line 331
由于 using
块,您在将 MemoryStream
传递给 FormFile
构造函数后隐式调用 Dispose
。然后你 return 一个 FormFile
对象的列表,其中内部流都是 disposed.
FormFile
不会复制流的内容,而是(通过 ReferencedReadStream
) calls methods like Stream.Read
which (in the case of MemoryStream
at least) will throw an exception if the stream has been closed——这是由于 Dispose
.
您没有提供任何信息,例如堆栈跟踪,所以这是一个最好的猜测,但是删除 using
块 应该 足以解决这个问题。
private List<IFormFile> Base64ToImage(List<EquipmentFile> equipmentFiles)
{
List<IFormFile> formFiles = new List<IFormFile>();
foreach (var eqp in equipmentFiles)
{
byte[] bytes = Convert.FromBase64String(eqp.File);
MemoryStream stream = new MemoryStream(bytes);
IFormFile file = new FormFile(stream, 0, bytes.Length, eqp.Name, eqp.Name);
formFiles.Add(file);
}
return formFiles;
}
我正在尝试将 base64String 列表转换为 IFormFile 列表。当我尝试 MemoryStream stream = new MemoryStream(bytes)
时,我得到了 stream.ReadTimeout SystemInvalidOperation
。那么为什么它不起作用?
这是我的代码:
private async Task<List<IFormFile>> Base64ToImage(List<EquipmentFile> equipmentFiles)
{
List<IFormFile> formFiles = new List<IFormFile>();
foreach (var eqp in equipmentFiles)
{
byte[] bytes = Convert.FromBase64String(eqp.File);
MemoryStream stream = new MemoryStream(bytes);
IFormFile file = new FormFile(stream, 0, bytes.Length, eqp.Name, eqp.Name);
formFiles.Add(file);
}
return formFiles;
}
我的堆栈跟踪:
System.InvalidOperationException: Timeouts are not supported on this stream.
at System.IO.Stream.get_ReadTimeout()
at FieldService.CallCenter.Api.Controllers.EquipmentLibraryController.Base64ToImage(List`1 equipmentFiles) in C:\Users\CMTech-DEV\Documents\cmtech\fieldservice-api\src\CallCenter\FieldService.CallCenter.API\Controllers\EquipmentLibraryController.cs:line 331
at System.IO.Stream.get_ReadTimeout()
at FieldService.CallCenter.Api.Controllers.EquipmentLibraryController.Base64ToImage(List`1 equipmentFiles) in C:\Users\CMTech-DEV\Documents\cmtech\fieldservice-api\src\CallCenter\FieldService.CallCenter.API\Controllers\EquipmentLibraryController.cs:line 331
由于 using
块,您在将 MemoryStream
传递给 FormFile
构造函数后隐式调用 Dispose
。然后你 return 一个 FormFile
对象的列表,其中内部流都是 disposed.
FormFile
不会复制流的内容,而是(通过 ReferencedReadStream
) calls methods like Stream.Read
which (in the case of MemoryStream
at least) will throw an exception if the stream has been closed——这是由于 Dispose
.
您没有提供任何信息,例如堆栈跟踪,所以这是一个最好的猜测,但是删除 using
块 应该 足以解决这个问题。
private List<IFormFile> Base64ToImage(List<EquipmentFile> equipmentFiles)
{
List<IFormFile> formFiles = new List<IFormFile>();
foreach (var eqp in equipmentFiles)
{
byte[] bytes = Convert.FromBase64String(eqp.File);
MemoryStream stream = new MemoryStream(bytes);
IFormFile file = new FormFile(stream, 0, bytes.Length, eqp.Name, eqp.Name);
formFiles.Add(file);
}
return formFiles;
}