Sisovin CHIENG

Sisovin CHIENG

  • NA
  • 3
  • 3.3k

Inputstream and contentlength is missing in microsoft.aspnet

Apr 1 2016 10:10 AM
As my review to the Microsoft.AspNet.Http.Abstractions, the IFormFile is missing the InputStream and ContentLength in ASP.NET MVC 6, it insteads of OpenReadStream, which is not functioned to respnse to the ConvertToBytes, for example:
  1. private byte[] ConvertToBytes(IFormFile image)  
  2. {  
  3. byte[] imageBytes = null;  
  4. //Then, Read stream in Binary  
  5. BinaryReader reader = new BinaryReader(image.InputStream);  
  6. imageBytes = reader.ReadBytes((int) image.ContentLength);  
  7. return imageBytes;  
  8.   

Do you know what happen to InputStream and ContentLength? In the IFormFile, are available of OpenReadStream and Length. I don't know how to use it. Do anyone know how to use it. I would like to have similar code as below example:
  1. private byte[] ConvertToBytes(IFormFile image)  
  2.         {  
  3.             byte[] imageBytes = null;  
  4.             //Then, Read stream in Binary  
  5.             BinaryReader reader = new BinaryReader(image.OpenReadStream());  
  6.             imageBytes = reader.ReadBytes((int) image.Length);  
  7.             return imageBytes;  
  8.               
  9.         } 
 What I have tried below code, but it does't work because of BinaryReader reader = new BinaryReader(image.OpenReadStream()) show up System.NullReferenceException was unhandled by user code with HResult=-2147467261
and the Message=Object reference not set to an instance of an object.:
  1. private byte[] ConvertToBytes(IFormFile image)  
  2.         {  
  3.             byte[] imageBytes = null;  
  4.             //Then, Read stream in Binary  
  5.             BinaryReader reader = new BinaryReader(image.OpenReadStream());  
  6.             imageBytes = reader.ReadBytes((int) image.Length);  
  7.             return imageBytes;  
  8.               
  9.         } 
 Can you help me to resolve this problem?


Answers (1)