Solved

Binary input stream to file

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

810 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