convert .pdf to a byte array

Discussion in 'ASP .Net' started by Angel Filev, Sep 15, 2005.

  1. Angel Filev

    Angel Filev Guest

    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);
    Angel Filev, Sep 15, 2005
    #1
    1. Advertising

  2. Angel Filev

    Joerg Jooss Guest

    Re: convert .pdf to a byte array

    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:
    Joerg Jooss, Sep 18, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kirby
    Replies:
    3
    Views:
    614
    Kirby
    Oct 8, 2004
  2. Shane Wealti

    convert byte[] to Byte[]

    Shane Wealti, Jun 13, 2005, in forum: Java
    Replies:
    5
    Views:
    42,045
    Brzezi
    Jun 13, 2005
  3. Replies:
    20
    Views:
    9,682
    licebmi
    Sep 7, 2009
  4. Polaris431
    Replies:
    8
    Views:
    731
    SM Ryan
    Dec 4, 2006
  5. M D

    convert 2 byte space to one byte

    M D, Oct 6, 2006, in forum: Javascript
    Replies:
    3
    Views:
    178
    Dr John Stockton
    Oct 6, 2006
Loading...

Share This Page