Extracting binary file from sql database image field to disk

G

Guest

I am trying to extract a zip file in a database image field to disk. For some
reason, the zip file is getting corrupted / truncated. I have code in ASP
which extracts the zip file no problem, so i know it is not corrupted in the
table. Any help would be appreciated.

FileStream fs = new FileStream(sFilePath + "TestFile.zip",
FileMode.CreateNew);
BinaryWriter bw = new BinaryWriter(fs);
byte[] buffer =
Encoding.ASCII.GetBytes(DataReader[sField].ToString());
bw.Write(buffer);
bw.Close();
fs.Close();
 
J

Jon Skeet [C# MVP]

Scott said:
I am trying to extract a zip file in a database image field to disk. For some
reason, the zip file is getting corrupted / truncated. I have code in ASP
which extracts the zip file no problem, so i know it is not corrupted in the
table. Any help would be appreciated.

FileStream fs = new FileStream(sFilePath + "TestFile.zip",
FileMode.CreateNew);
BinaryWriter bw = new BinaryWriter(fs);
byte[] buffer =
Encoding.ASCII.GetBytes(DataReader[sField].ToString());
bw.Write(buffer);
bw.Close();
fs.Close();

Don't convert the binary data to a string - that's a recipe for a
disaster.

If you look at DataReader[sField].GetType(), what does it say?
 
J

Jon Skeet [C# MVP]

Matt F said:
I use the binaryformatter class for this type of thing --- MSDN example for
the binaryformatter class has a pretty good example.

It's a zip file - natural binary data to start with. Serializing it
would add an extra layer for no good reason, *and* make it
hard/impossible to use from other platforms.
 
G

Guest

Jon Skeet said:
Scott said:
I am trying to extract a zip file in a database image field to disk. For some
reason, the zip file is getting corrupted / truncated. I have code in ASP
which extracts the zip file no problem, so i know it is not corrupted in the
table. Any help would be appreciated.

FileStream fs = new FileStream(sFilePath + "TestFile.zip",
FileMode.CreateNew);
BinaryWriter bw = new BinaryWriter(fs);
byte[] buffer =
Encoding.ASCII.GetBytes(DataReader[sField].ToString());
bw.Write(buffer);
bw.Close();
fs.Close();

Don't convert the binary data to a string - that's a recipe for a
disaster.

If you look at DataReader[sField].GetType(), what does it say?
The DataReader[sField].GetType() comes back as System.String
 
J

Jon Skeet [C# MVP]

Scott said:
If you look at DataReader[sField].GetType(), what does it say?
The DataReader[sField].GetType() comes back as System.String

Okay, that's a bad start. What's the data type in the database, and how
have you stored the zip file in there in the first place? Storing a
binary block of data (such as a zip file) in a text field is a really
bad move...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top