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

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.

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

Posted 05 October 2012 - 01:59 PM

Skydiver, 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

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.

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

Posted 05 October 2012 - 02:47 PM

Skydiver, 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.

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.)

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

Posted 05 October 2012 - 03:26 PM

Skydiver, 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 ???

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.