convert .pdf to a byte array

A

Angel Filev

Hi everyone,
I am trying to store a file as a binary array in an "image" field in SQL
Server 2000 database.
It works OK except for the ".PDF" files, which I believe get corrupted in
the
process of reading a stream to a byte array.
Uploading and downloading seems to work fine, but "Acrobat" pop ups
"The file is damaged and could not be repaired" error.
Is there any way I can make this to work.

Following is a pseudo code of what I am trying to accomplish.

//Uploading a file browser->web server
//get file size (theFile is a VS "file field" component )
int fileSize = theFile.PostedFile.ContentLength;
//get file as binary stream
Stream fileStream = theFile.PostedFile.InputStream;
//create byte array to keep file as bytes
byte[] bArray = new byte[fileSize];
//load array from stream
fileStream.Read(bArray , 0, size);
//At this point bArray is saved to DataBase

//Downloading file
//read image field from database and typecast as byte array
byte [] bArray = GetBinaryArrayFromDB()
string fileName = GetFileName();
string fileType = GetMIMEFileType();

//Send file to the browser
Response.AddHeader("Content-Disposition", "attachment; filename=" +
fileName);
Response.ContentType = fileType;
Response.BinaryWrite(bArray);
 
J

Joerg Jooss

Angel said:
Hi everyone,
I am trying to store a file as a binary array in an "image" field in
SQL Server 2000 database. It works OK except for the ".PDF" files,
which I believe get corrupted in the process of reading a stream to a
byte array. Uploading and downloading seems to work fine, but
"Acrobat" pop ups "The file is damaged and could not be repaired"
error. Is there any way I can make this to work.

Following is a pseudo code of what I am trying to accomplish.

//Uploading a file browser->web server
//get file size (theFile is a VS "file field" component )
int fileSize = theFile.PostedFile.ContentLength;
//get file as binary stream
Stream fileStream = theFile.PostedFile.InputStream;
//create byte array to keep file as bytes
byte[] bArray = new byte[fileSize];
//load array from stream
fileStream.Read(bArray , 0, size);
//At this point bArray is saved to DataBase

//Downloading file
//read image field from database and typecast as byte array
byte [] bArray = GetBinaryArrayFromDB()
string fileName = GetFileName();
string fileType = GetMIMEFileType();

//Send file to the browser
Response.AddHeader("Content-Disposition", "attachment; filename=" +
fileName); Response.ContentType = fileType;
Response.BinaryWrite(bArray);

Your not-so-pseudo-code looks ok ;-)

What happens if you set a Content-Length header with the value of
bArray.Length? Older Acrobat plugins require Content-Length.

Cheers,
 

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

Members online

Forum statistics

Threads
474,034
Messages
2,570,356
Members
47,002
Latest member
RobertoLip

Latest Threads

Top