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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top