8 Replies - 2108 Views - Last Post: 05 October 2012 - 03:52 PM Rate Topic: -----

#1 sameer1988  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 05-October 12

Convert uploaded file into stream...is this right way?

Posted 05 October 2012 - 12:19 PM

System.IO.Stream myStream;
                    Int32 fileLen;
                    Stream myStream1 = new MemoryStream();


                    // Get the length of the file.
                    fileLen = FileUpload1.PostedFile.ContentLength;



                    // Create a byte array to hold the contents of the file.
                    Byte[] Input = new Byte[fileLen];


                    UnicodeEncoding uniEncoding = new UnicodeEncoding();


                    // Initialize the stream to read the uploaded file.
                    myStream = FileUpload1.FileContent;

                    // Read the file into the byte array.
                    myStream.Read(Input, 0, fileLen);


                    string value = ASCIIEncoding.ASCII.GetString(Input);


                    using (StreamWriter sw = new StreamWriter(myStream1, uniEncoding))
                    {
                        sw.Write(value);
                    }

                    //myStream will be the output

System.IO.Stream myStream;
                    Int32 fileLen;
                    Stream myStream1 = new MemoryStream();


                    // Get the length of the file.
                    fileLen = FileUpload1.PostedFile.ContentLength;



                    // Create a byte array to hold the contents of the file.
                    Byte[] Input = new Byte[fileLen];


                    UnicodeEncoding uniEncoding = new UnicodeEncoding();


                    // Initialize the stream to read the uploaded file.
                    myStream = FileUpload1.FileContent;

                    // Read the file into the byte array.
                    myStream.Read(Input, 0, fileLen);


                    string value = ASCIIEncoding.ASCII.GetString(Input);



                    using (StreamWriter sw = new StreamWriter(myStream1, uniEncoding))
                    {
                        sw.Write(value);
                    }

                    //myStream1 will be output

This post has been edited by AdamSpeight2008: 05 October 2012 - 12:30 PM
Reason for edit:: Added in code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Convert uploaded file into stream...is this right way?

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Re: Convert uploaded file into stream...is this right way?

Posted 05 October 2012 - 12:30 PM

Nope because it in the vb.net section.
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3168
  • View blog
  • Posts: 9,581
  • Joined: 05-May 12

Re: Convert uploaded file into stream...is this right way?

Posted 05 October 2012 - 01:01 PM

You already have a stream in FileUpload1.FileContent. Why do you need to convert it to a stream?

If your intent was to convert ASCII data that was uploaded into Unicode, you can simply create one StreamReader with the ASCII encoding (http://msdn.microsoft.com/en-us/library/ms143456.aspx), and a StreamWriter with the Unicode encoding (http://msdn.microsoft.com/en-us/library/3aadshsx.aspx). Then you can read and write a line at a time instead of allocating two huge memory streams.
Was This Post Helpful? 0
  • +
  • -

#4 sameer1988  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 05-October 12

Re: Convert uploaded file into stream...is this right way?

Posted 05 October 2012 - 01:59 PM

View PostSkydiver, on 05 October 2012 - 01:01 PM, said:

You already have a stream in FileUpload1.FileContent. Why do you need to convert it to a stream?

If your intent was to convert ASCII data that was uploaded into Unicode, you can simply create one StreamReader with the ASCII encoding (http://msdn.microsoft.com/en-us/library/ms143456.aspx), and a StreamWriter with the Unicode encoding (http://msdn.microsoft.com/en-us/library/3aadshsx.aspx). Then you can read and write a line at a time instead of allocating two huge memory streams.


I used stream = FileUpload1.FileContent
then when I have written that stream to response.stream to check the output
(uploaded file should be same as the above stream i get the output as--- System.Web.HttpInputStream in file)

but if I am converting in to string with all the above conversions, and then I download the stream the content of the stream is same as the file uploaded
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3168
  • View blog
  • Posts: 9,581
  • Joined: 05-May 12

Re: Convert uploaded file into stream...is this right way?

Posted 05 October 2012 - 02:30 PM

Have you done a hex dump of the stream data or are you just looking at the data in notepad or in a message box or HTML renderer? Notepad tends to hide whether a file in Unicode or ANSI unless you really dig. The message box gives you no clue whether the string is on format or another originally bceause what gets displayed on screen will be Unicode. And the HTML that is rendered is sent back down as UTF-8 encoded, is it may have round tripped from ASCII to Unicode to UTF-8.
Was This Post Helpful? 0
  • +
  • -

#6 sameer1988  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 05-October 12

Re: Convert uploaded file into stream...is this right way?

Posted 05 October 2012 - 02:47 PM

View PostSkydiver, on 05 October 2012 - 02:30 PM, said:

Have you done a hex dump of the stream data or are you just looking at the data in notepad or in a message box or HTML renderer? Notepad tends to hide whether a file in Unicode or ANSI unless you really dig. The message box gives you no clue whether the string is on format or another originally bceause what gets displayed on screen will be Unicode. And the HTML that is rendered is sent back down as UTF-8 encoded, is it may have round tripped from ASCII to Unicode to UTF-8.


1. By above string conversion and generating stream. I am passing this stream to the function.
2. the function accepts only stream.
3. If I pass the stream in this way do you think it will work i.e the function will get exactly the contents of uploaded file.
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3168
  • View blog
  • Posts: 9,581
  • Joined: 05-May 12

Re: Convert uploaded file into stream...is this right way?

Posted 05 October 2012 - 03:18 PM

If you pass the stream directly to the function, it will get the exact data that was uploaded. (It is the StreamReader class that does decoding of the bytes to figure out any Unicode/Multibyte that needs any special treatment.)
Was This Post Helpful? 0
  • +
  • -

#8 sameer1988  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 05-October 12

Re: Convert uploaded file into stream...is this right way?

Posted 05 October 2012 - 03:26 PM

View PostSkydiver, on 05 October 2012 - 03:18 PM, said:

If you pass the stream directly to the function, it will get the exact data that was uploaded. (It is the StreamReader class that does decoding of the bytes to figure out any Unicode/Multibyte that needs any special treatment.)



so you are saying i should directly pass the below stream to my function ???

for example:
stream = FileUpload1.content;
function xyz(stream);

no need of all conversions from string to stream ???
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3168
  • View blog
  • Posts: 9,581
  • Joined: 05-May 12

Re: Convert uploaded file into stream...is this right way?

Posted 05 October 2012 - 03:52 PM

If function xyz() takes a stream, and function xyz() doesn't have any requirements that the data in the stream be encoded in any particular format, just pass it along. Why waste the CPU cycles and memory?

On the other hands if function xyz() takes a stream, but it assumes that the data in stream is encoded as UTF-16, but stream contains ASCII, then you need to do the conversion. Or even better wrap the ASCII stream with a stream that returns UTF-16 as the bytes are read, but that would be a more advanced technique if you are really strapped for server memory.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1