?
Solved

Binary input stream to file

Posted on 2006-07-12
2
Medium Priority
?
449 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 2000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

864 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