Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to cast object of type 'System.Web.HttpInputStream' to type 'System.IO.FileStream' MVC 3

I have met an issue regarding the casting type from HttpInputStream to FileStream.

How I did ?

I have a HttpPostedFileBase object and I want to have FileStream.

I wrote:

public void Test(HttpPostedFileBase postedFile) {
  FileStream fileStream = (FileStream)(postedFile.InputStream); // throw exception

  FileStream anotherFileStream = postedFile.InputStream as FileStream; // null
}

I tried also

public void Test(HttpPostedFileBase postedFile) {
  Stream stream = postedFile.InputStream as Stream;

  FileStream myFile = (FileStream)stream;

}

But no success.

Why at postedFile.InputStream comes HttpInputStream type ?

And how could I solve this issue ?

Thanks

like image 427
Snake Eyes Avatar asked Oct 29 '12 10:10

Snake Eyes


1 Answers

public byte[] LoadUploadedFile(HttpPostedFileBase uploadedFile)
{
    var buf = new byte[uploadedFile.InputStream.Length];
    uploadedFile.InputStream.Read(buf, 0, (int)uploadedFile.InputStream.Length);
    return buf;
}
like image 93
yarg Avatar answered Oct 18 '22 01:10

yarg