Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > convert .pdf to a byte array

Reply
Thread Tools

convert .pdf to a byte array

 
 
Angel Filev
Guest
Posts: n/a
 
      09-15-2005
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);


 
Reply With Quote
 
 
 
 
Joerg Jooss
Guest
Posts: n/a
 
      09-18-2005
Angel Filev wrote:

> 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,
--
http://www.joergjooss.de
mailto:news-
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
convert form byte[4] to Int32 while retaining the binary value of the byte array jeff@foundrymusic.com C++ 20 09-07-2009 08:54 PM
How to convert a 4 byte character string to its equivalent 4 byte integer value? Polaris431 C Programming 8 12-04-2006 07:00 AM
convert 2 byte space to one byte M D Javascript 3 10-06-2006 07:39 PM
convert byte[] to Byte[] Shane Wealti Java 5 06-13-2005 09:16 PM
Converting a Primative byte array to a Byte array object Kirby Java 3 10-08-2004 03:01 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57