saving pdf file to sql server

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default saving pdf file to sql server

    Hi group,

    I have an web application where the user can upload a pdf file. This file is
    stored in a table, in a column of type ntext.
    The user can later request the content of this column, and the application
    should open a new page and load the pdf content.

    My problem:
    If I read the uploaded pdf file into a string variable and then I
    response.write it right away to another page, it shows fine (I can see a pdf
    document opened with acrobat reader).
    But if I save the string in the sql database and then later retreive it from
    there, it will not show anymore.
    It seems like it is being somehow transformed during saving to/retreiving
    from the sqlserver ...

    Any ideas ?

    thanks,
    Andrei.

    Following is the code I use. The commented block sends back the pdf info to
    the browser.
    Dim mf As HttpPostedFile = myfile.PostedFile

    Dim strFile As String = myfile.Value

    Dim nFileLen As Integer = mf.ContentLength

    Dim b(nFileLen) As Byte

    mf.InputStream.Read(b, 0, nFileLen)

    Dim str As String = Encoding.Unicode.GetChars(b)

    str = Replace(str, "'", "''")

    'str = Replace(str, "''", "'")

    'Dim b1() As Byte

    'b1 = Encoding.Unicode.GetBytes(str)

    'Response.ContentType = "application/pdf"

    'Response.BinaryWrite(b1)

    'Response.Flush()

    'Response.Close()

    'save file to DB

    Dim cn As New SqlClient.SqlConnection("server=(local);initial
    catalog=atest;integrated security=SSPI;")

    cn.Open()

    Dim cmd As New SqlClient.SqlCommand("insert into cust (name, info) values("
    & _

    "'" & strFile & "','" & str & "')", cn)

    cmd.ExecuteNonQuery()

    ...........
    Dim cmd As New SqlCommand("select name,info from cust where nume='" &
    cboFis.SelectedValue & "'", cn)

    Dim dr As SqlDataReader

    dr = cmd.ExecuteReader()

    Dim str As String

    Dim b() As Byte

    If dr.Read() Then

    str = dr("info")

    str = Replace(str, "''", "'")

    b = Encoding.Unicode.GetBytes(str)

    Response.ClearContent()

    Response.ClearHeaders()

    Response.ContentType = "application/pdf"

    Response.BinaryWrite(b)

    Response.Flush()

    Response.Close()

    End If



    andy_ro Guest

  2. Similar Questions and Discussions

    1. Saving/uploading a file to seperate server
      I am trying to figure out a way to save a file to a seperate server(not my web server) on my network. Is this possible? If so with what tag? ...
    2. saving files inn CS to server
      having trouble saving files in photoshop CS to a server, it gives me a message saying cannot save because of disk error, but it does save it anyway....
    3. Saving of an swf file to a server
      Hi all, Thank you for the reply. I understand that saving the image of the changed swf file onto the server is not something which is simple. But...
    4. Getting HTML content from one Server and Saving it on another Server
      Hi everyone, I'm incharge of updating the stats on a chl hockey teams Web Site. I would like to know if there is a way to be able to save the...
    5. Saving to a file server (XP and PS7)
      Search trough the forum and you'll find this is a very common problem that many people are having. Best advice I can give, only try saving with one...
  3. #2

    Default Re: saving pdf file to sql server

    Hi,

    I think it is better to save the PDF file as binary data in the SQL
    server instead of translating it from / to string.

    Natty Gur, CTO
    Dao2Com Ltd.
    28th Baruch Hirsch st. Bnei-Brak
    Israel , 51114

    Phone Numbers:
    Office: +972-(0)3-5786668
    Fax: +972-(0)3-5703475
    Mobile: +972-(0)58-888377

    Know the overall picture


    *** Sent via Developersdex [url]http://www.developersdex.com[/url] ***
    Don't just participate in USENET...get rewarded for it!
    Natty Gur Guest

  4. #3

    Default Re: saving pdf file to sql server

    use the Image datatype in sqlserver. Put the .InputStream from the
    PostedFile into a byte array. The byte array will be your parameter into
    the stored proc that takes an image datatype. Careful if you happen to be
    deriving parameters from the proc...as .net will not type the Image SqlType
    correctly....I believe it makes it ntext or something....

    ~PJ

    "andy_ro" <andy_ro@videotron.ca> wrote in message
    news:O9OaIpsODHA.2476@TK2MSFTNGP10.phx.gbl...
    > Hi group,
    >
    > I have an web application where the user can upload a pdf file. This file
    is
    > stored in a table, in a column of type ntext.
    > The user can later request the content of this column, and the application
    > should open a new page and load the pdf content.
    >
    > My problem:
    > If I read the uploaded pdf file into a string variable and then I
    > response.write it right away to another page, it shows fine (I can see a
    pdf
    > document opened with acrobat reader).
    > But if I save the string in the sql database and then later retreive it
    from
    > there, it will not show anymore.
    > It seems like it is being somehow transformed during saving to/retreiving
    > from the sqlserver ...
    >
    > Any ideas ?
    >
    > thanks,
    > Andrei.
    >
    > Following is the code I use. The commented block sends back the pdf info
    to
    > the browser.
    > Dim mf As HttpPostedFile = myfile.PostedFile
    >
    > Dim strFile As String = myfile.Value
    >
    > Dim nFileLen As Integer = mf.ContentLength
    >
    > Dim b(nFileLen) As Byte
    >
    > mf.InputStream.Read(b, 0, nFileLen)
    >
    > Dim str As String = Encoding.Unicode.GetChars(b)
    >
    > str = Replace(str, "'", "''")
    >
    > 'str = Replace(str, "''", "'")
    >
    > 'Dim b1() As Byte
    >
    > 'b1 = Encoding.Unicode.GetBytes(str)
    >
    > 'Response.ContentType = "application/pdf"
    >
    > 'Response.BinaryWrite(b1)
    >
    > 'Response.Flush()
    >
    > 'Response.Close()
    >
    > 'save file to DB
    >
    > Dim cn As New SqlClient.SqlConnection("server=(local);initial
    > catalog=atest;integrated security=SSPI;")
    >
    > cn.Open()
    >
    > Dim cmd As New SqlClient.SqlCommand("insert into cust (name, info)
    values("
    > & _
    >
    > "'" & strFile & "','" & str & "')", cn)
    >
    > cmd.ExecuteNonQuery()
    >
    > ..........
    > Dim cmd As New SqlCommand("select name,info from cust where nume='" &
    > cboFis.SelectedValue & "'", cn)
    >
    > Dim dr As SqlDataReader
    >
    > dr = cmd.ExecuteReader()
    >
    > Dim str As String
    >
    > Dim b() As Byte
    >
    > If dr.Read() Then
    >
    > str = dr("info")
    >
    > str = Replace(str, "''", "'")
    >
    > b = Encoding.Unicode.GetBytes(str)
    >
    > Response.ClearContent()
    >
    > Response.ClearHeaders()
    >
    > Response.ContentType = "application/pdf"
    >
    > Response.BinaryWrite(b)
    >
    > Response.Flush()
    >
    > Response.Close()
    >
    > End If
    >
    >
    >

    PJ Guest

  5. #4

    Default Re: saving pdf file to sql server

    Thank you, Natty and PJ,

    I used an Image field in SQLServer with a stored procedure that writes to it
    and it works perfect.

    Andrei.



    "andy_ro" <andy_ro@videotron.ca> wrote in message
    news:O9OaIpsODHA.2476@TK2MSFTNGP10.phx.gbl...
    > Hi group,
    >
    > I have an web application where the user can upload a pdf file. This file
    is
    > stored in a table, in a column of type ntext.
    > The user can later request the content of this column, and the application
    > should open a new page and load the pdf content.
    >
    > My problem:
    > If I read the uploaded pdf file into a string variable and then I
    > response.write it right away to another page, it shows fine (I can see a
    pdf
    > document opened with acrobat reader).
    > But if I save the string in the sql database and then later retreive it
    from
    > there, it will not show anymore.
    > It seems like it is being somehow transformed during saving to/retreiving
    > from the sqlserver ...
    >
    > Any ideas ?
    >
    > thanks,
    > Andrei.
    >
    > Following is the code I use. The commented block sends back the pdf info
    to
    > the browser.
    > Dim mf As HttpPostedFile = myfile.PostedFile
    >
    > Dim strFile As String = myfile.Value
    >
    > Dim nFileLen As Integer = mf.ContentLength
    >
    > Dim b(nFileLen) As Byte
    >
    > mf.InputStream.Read(b, 0, nFileLen)
    >
    > Dim str As String = Encoding.Unicode.GetChars(b)
    >
    > str = Replace(str, "'", "''")
    >
    > 'str = Replace(str, "''", "'")
    >
    > 'Dim b1() As Byte
    >
    > 'b1 = Encoding.Unicode.GetBytes(str)
    >
    > 'Response.ContentType = "application/pdf"
    >
    > 'Response.BinaryWrite(b1)
    >
    > 'Response.Flush()
    >
    > 'Response.Close()
    >
    > 'save file to DB
    >
    > Dim cn As New SqlClient.SqlConnection("server=(local);initial
    > catalog=atest;integrated security=SSPI;")
    >
    > cn.Open()
    >
    > Dim cmd As New SqlClient.SqlCommand("insert into cust (name, info)
    values("
    > & _
    >
    > "'" & strFile & "','" & str & "')", cn)
    >
    > cmd.ExecuteNonQuery()
    >
    > ..........
    > Dim cmd As New SqlCommand("select name,info from cust where nume='" &
    > cboFis.SelectedValue & "'", cn)
    >
    > Dim dr As SqlDataReader
    >
    > dr = cmd.ExecuteReader()
    >
    > Dim str As String
    >
    > Dim b() As Byte
    >
    > If dr.Read() Then
    >
    > str = dr("info")
    >
    > str = Replace(str, "''", "'")
    >
    > b = Encoding.Unicode.GetBytes(str)
    >
    > Response.ClearContent()
    >
    > Response.ClearHeaders()
    >
    > Response.ContentType = "application/pdf"
    >
    > Response.BinaryWrite(b)
    >
    > Response.Flush()
    >
    > Response.Close()
    >
    > End If
    >
    >
    >

    andrei Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139