Wednesday, March 28, 2012

System.IO.Stream to resulting file.

I'm kicking myself as I've had this 'bug' before, and I cannot remember what it was that resolved it.

Essentially I'm re-writing my upload control, and its started exhibiting a weird 'corruption' thing.

Essentially I have this code:

Dim FileLenAs Integer = FileData.LengthDim FileDataByes(FileLen)As Byte' Initialize the stream.Dim ResourceStreamAs System.IO.Stream = FileData' Read the file into the byte array. ResourceStream.Read(FileDataByes, 0, FileLen)Dim SavingStreamAs System.IO.FileStream = _ System.IO.File.Create(Server.MapPath(Me.__VirtualPath & FileName))' Copy the byte array into a string.For iAs Integer = 0To FileLen - 1 SavingStream.WriteByte(FileDataByes(i).ToString())Next' Close the streams. ResourceStream.Close() SavingStream.Close()
FileData is a stream of the file, either from a server side file, or from a httppostedfile. The file appears to save ok, the size of the file is correct. And when checking the properties the byte count looks exactly the same.

But the file is quite obviously corrected, it wont open. I'm guessing the issue is to do with encoding. But I can't remember what I did to resolve it. I'm looking at my old framework and the code at this point is identical. Could it be something to do server settings changing the encoding of POSTS?

Pre-cheers.

Stevo.
Scratch that, its the literal 5 minutes after submitted a thread you realise your obvious mistake...

' Create the fragment.Dim FragmentAs String = _ Engine.Convert.ToHexString(FileData, 0, 2)
DOH! can't believe I forget to 'rewind' the stream after getting a fragment identifier...

Cheers anyways!

0 comments:

Post a Comment