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

7

18

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

Porpoise answered 29/10, 2012 at 10:31 Comment(1)
Your file base InputStream (incomming stream) is HttpInputStream, not FileStream. You have to read from it and save it somewhere, maybe to your local file. For this you can use FileStream.Lundeen
E
11

The stream that you get from your HTTP call is read-only sequential (non-seekable) and the FileStream is read/write seekable. You will need first to read the entire stream from the HTTP call into a byte array, then create the FileStream from that array.

Exhalant answered 29/10, 2012 at 10:37 Comment(2)
An example can you provide ?Porpoise
An accepted answer with no code example... UghCulvert
I
19
public byte[] LoadUploadedFile(HttpPostedFileBase uploadedFile)
{
    var buf = new byte[uploadedFile.InputStream.Length];
    uploadedFile.InputStream.Read(buf, 0, (int)uploadedFile.InputStream.Length);
    return buf;
}
Isaiasisak answered 24/5, 2013 at 16:5 Comment(0)
E
11

The stream that you get from your HTTP call is read-only sequential (non-seekable) and the FileStream is read/write seekable. You will need first to read the entire stream from the HTTP call into a byte array, then create the FileStream from that array.

Exhalant answered 29/10, 2012 at 10:37 Comment(2)
An example can you provide ?Porpoise
An accepted answer with no code example... UghCulvert
T
11

I used the following and it worked just fine for the same situation

MemoryStream streamIWant = new MemoryStream();
using (Stream mystream = (Stream)AmazonS3Service.GetObjectStream(AWSAlbumBucketName, ObjectId))                                                            
{
    mystream.CopyTo(streamIWant);
}
return streamIWant;

The GetObjectStream returns the same type of string mentioned in the question.

Target answered 20/2, 2014 at 3:44 Comment(0)
D
1

You can use the .SaveAs method to save the file content. HttpInputSteam probably because it's uploaded through http [browser]

 postedFile.SaveAs("Full Path to file name");

You can also use CopyTo

FileStream f = new FileStream(fullPath, FileMode.CreateNew);
postedFile.InputStream.CopyTo(f);
f.Close();
Durante answered 29/10, 2012 at 10:36 Comment(0)
F
0

Below code worked for me..

Use a BinaryReader object to return a byte array from the stream like:

byte[] fileData = null;
using (var binaryReader = new BinaryReader(Request.Files[0].InputStream))
{
    fileData = binaryReader.ReadBytes(Request.Files[0].ContentLength);
}

How to create byte array from HttpPostedFile

Faith answered 21/6, 2017 at 12:35 Comment(0)
I
0

It will work for you. IFormFile file;

        if (file != null)
        {
            byte[]? image = Array.Empty<byte>();
            await using var memoryStream = new MemoryStream();
            await file!.CopyToAsync(memoryStream);
            image = memoryStream.ToArray();
        }
Inocenciainoculable answered 3/9, 2021 at 7:56 Comment(0)
S
0

First convert to byte array then convert to Stream.

[HttpPost]
[Route("api/TestReader/UploadTestReaderStudentsGrade")]
public IHttpActionResult UploadTestReaderStudentsGrade()
{
    HttpPostedFile httpPostedFile = HttpContext.Current.Request.Files[0];
    var fileBytes = new byte[httpPostedFile.InputStream.Length];
    httpPostedFile.InputStream.Read(fileBytes, 0, (int)httpPostedFile.InputStream.Length); // to byte array
    Stream stream = new MemoryStream(fileBytes); // to Stream

    Workbook workbook = new Workbook(stream);
    return Ok();
}
Strophanthus answered 6/7, 2022 at 4:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.