Solved

Binary input stream to file

Posted on 2006-07-12
2
444 Views
Last Modified: 2008-02-01
I want to read the input stream (HTTP POST to an aspx page) and write it to a file on the web server.  Want it to be binary friendly.  This is my amateur attempt to write the code...

            System.IO.StreamReader sr = new System.IO.StreamReader(Request.InputStream);
            System.IO.StreamWriter sw = System.IO.File.WriteAllBytes(appSettings["fileDirectory"] + fileName, sr.ReadToEnd);

It has some type and overloading problems.  Any suggestions?  Thanks a lot.
0
Comment
Question by:jschnei555
2 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 17097945
Ok, so for this ASP.NET 2.0 problem, here is what I would suggest:

      using (BinaryReader br = new BinaryReader(Request.InputStream))
      {

        int length = (int)Request.InputStream.Length;

        byte[] buffer = br.ReadBytes(length);

        File.WriteAllBytes(appSettings["fileDirectory"] + fileName, buffer) ;
      }

1) The using block will Dispose of the reader implicitly at the completion of the block

2) Use a BinaryReader to get the bytes

3) Write the bytes to the file, without the StreamWriter.

Bob
0
 

Author Comment

by:jschnei555
ID: 17098563
Works perfectly.  Clean and simple code too.  Thanks.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question