Solved

Binary input stream to file

Posted on 2006-07-12
2
434 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Voice recognition ASP or ASP.NET or JavaScript 2 44
Expando 4 33
Image(2) 3 22
String manipulation 15 49
Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now