I have a situation where I need to create the FormFile from an image and then I need back MemoryStream from FormFile. The MemoryStream from a file is in some other place. I have just made this as a sample to produce the issue.
private IFormFile ReturnFormFile(Image image, string thumbnailName)
{
IFormFile file = null;
using (MemoryStream ms = new MemoryStream())
{
image.Save(ms, ImageFormat.Jpeg);
file = new FormFile(ms, 0, ms.Length, "name", thumbnailName);
ms.Seek(0, SeekOrigin.Begin);
}
using (System.IO.MemoryStream memStream = new System.IO.MemoryStream())
{
file.CopyTo(memStream);// System.ObjectDisposedException: 'Cannot access a closed Stream.'
Byte[] fileData = memStream.ToArray();
}
return file;
}
Any suggestions please.
Try this way:
private IFormFile ReturnFormFile(Image image, string thumbnailName)
{
IFormFile file = null;
using (MemoryStream ms = new MemoryStream())
{
image.Save(ms, ImageFormat.Jpeg);
file = new FormFile(ms, 0, ms.Length, "name", thumbnailName);
ms.Seek(0, SeekOrigin.Begin);
using (System.IO.MemoryStream memStream = new System.IO.MemoryStream())
{
file.CopyTo(memStream);// System.ObjectDisposedException: 'Cannot access a closed Stream.'
Byte[] fileData = memStream.ToArray();
}
return file;
}
}