Hello,
Can someone point me in the right direction. I'm trying to save a PDF document to a MS Sql database. I can accomplish this if I save the document to the hard drive first, read the file BACK into memory, save to the DB. Then I can read from DB and save back to the hard drive. Everything great. But I would really like to skip the step of saving to hard drive first. However when I use a Memory Steam "pdfdoc.save(memorystream)" I get a byte array that is larger than the saved document and when I write it straight to disk it is garbage, so there is something I'm messing up somewhere. Here is a code snippet (let me know if there is anything obvious):
Dim mStream_ As New IO.MemoryStream()
'This is a test document about 81KB in size.
_ioPDFDoc_.Save(mStream_)
Dim lByteArr_ As Byte()
lByteArr_ = mStream_.GetBuffer
'NOTE: I'm getting back 214K back from the memorystream????? I'm not expert but would expect about 81K
'update db
loCmd_ = New SqlCommand
loCmd_.Connection = loCxn_
loCmd_.CommandTimeout = 180 'waits 3 minutes before timing out with error
loCmd_.CommandType = CommandType.Text
loCmd_.CommandText = "Update dbo.cs_ntis set Statements_pdf = @Statements_pdf where id = @id "
loCmd_.Parameters.Add("@id", SqlDbType.Decimal)
loCmd_.Parameters("@id").Value = adCsNtisID_
Dim P_ As New SqlParameter("@statements_pdf", SqlDbType.Binary, lByteArr_.Length, ParameterDirection.Input, False, 0, 0, Nothing, DataRowVersion.Current, lByteArr_)
loCmd_.Parameters.Add(P_)
loCmd_.ExecuteNonQuery()
'Test the blob update
loCmd_ = New SqlCommand
loCmd_.Connection = loCxn_
loCmd_.CommandTimeout = 180 'waits 3 minutes before timing out with error
loCmd_.CommandType = CommandType.Text
loCmd_.CommandText = "select Statements_pdf from dbo.cs_ntis where id = @id "
loCmd_.Parameters.Add("@id", SqlDbType.Decimal)
loCmd_.Parameters("@id").Value = adCsNtisID_
ldr_ = loCmd_.ExecuteReader
ldr_.Read()
Dim b2_(CInt(ldr_.GetBytes(0, 0, Nothing, 0, Integer.MaxValue) - 1)) As Byte
ldr_.GetBytes(0, 0, b2_, 0, b2_.Length)
Dim lfs_ As New System.IO.FileStream("C:\temp\NtisFromDb.pdf", IO.FileMode.Create, IO.FileAccess.Write)
lfs_.Write(b2_, 0, b2_.Length)
lfs_.Close()
'When I look at the file on the HD the file won't open it is corrupted. If I just use the pdfdoc.save("C:\temp\ntis.pdf") the document is perfect.
Any help appreciated,
James.