473,320 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

error downloading .pdf from SQL database

Hi all,

Well, thanks to some smart/helpful people here my upload and download
section of my website works great! I am allowing users to upload
directly into the SQL database but files cannot exceed 1 Meg (actually
1.2M but I tell them 1 meg) and a max of five uploads per incident (it
is a security incident reporting system)

I do not want to redesign this now that it is working, but I cannot
download pdfs! Acrobat tells me the file is corrupt (reader version
6). This happens if I open or save to disk and open later. Every other
file type I have tried works great.

As users upload attachments, a datagrid appears with the option to
view or delete attachments. Below is the code to view (which works for
all but pdf) and the next sub is the upload routine (might be
important, eh). Forgive my coding...I am new and self-taught as you
will undoubtedly notice :) Thanks in advance.

Sub dgrdAttachment_click (sender As Object, e as
DataGridCommandEventArgs)
Dim conMatt_I3 As SqlConnection
conMatt_I3 = New SqlConnection(
"Server=localhost;uid=sa;pwd=sa;database=Matt_ I3" )

conMatt_I3.Open()

If e.commandname="View" Then

Dim buttonColumn as TableCell = e.Item.Cells(0)
viewstate ("attachment_id") = buttonColumn.text

Dim cmdLoadAttachment As SqlCommand
Dim dtrLoadAttachment As SqlDataReader

cmdLoadAttachment = New SqlCommand ("SELECT * FROM attachment WHERE
attachment_id='" & viewstate ("attachment_id") & "'", conMatt_I3)
dtrLoadAttachment = cmdLoadAttachment.ExecuteReader()
dtrLoadAttachment.Read()

Response.ContentType =
dtrLoadAttachment("Attachment_ContentType").ToStri ng
Response.Outputstream.Write(Ctype(dtrLoadAttachmen t("Attachment_FileData"),
Byte() ), 0, Cint(dtrLoadAttachment("Attachment_FileSize")))
Response.AddHeader("Content-Disposition", "attachment;filename=" +
dtrLoadAttachment("Attachment_FileName").ToString( ))

End If
-----------------------------------------------------------------

Sub attachmentSubmit_Click ( s As Object, e As EventArgs )
Dim conMatt_I3 As SqlConnection
Dim cmdCheckFiveAttachments As SqlCommand
Dim cmdInsertAttachment As SqlCommand
Dim intCheckFiveAttachments

conMatt_I3 = New SqlConnection(
"Server=localhost;uid=sa;pwd=sa;database=Matt_ I3" )
conMatt_I3.Open()

cmdCheckFiveAttachments = New SqlCommand ("SELECT Count(*) FROM
attachment WHERE incident_number='" & viewstate
("intMaxIncidentNumber") & "'", conMatt_I3)

intCheckFiveAttachments = cmdCheckFiveAttachments.ExecuteScalar()
If intCheckFiveAttachments = 5 Then
lblError.text = "Only five attachments are allowed per Incident.
Please delete one and try again."
Else

cmdInsertAttachment = New SqlCommand ("Insert Attachment
(attachment_filename, attachment_description, incident_number,
attachment_filesize, attachment_filedata, attachment_contenttype)
Values ( @attachment_filename, @attachment_description,
@incident_number, @attachment_filesize, @attachment_filedata,
@attachment_contenttype )", conMatt_I3)
Dim iLength As Integer = CType(File1.PostedFile.InputStream.Length,
Integer)
If iLength = 0 Then Exit Sub 'not a valid file
Dim sContentType As String = File1.PostedFile.ContentType
Dim sFileName As String, i As Integer
Dim bytContent As Byte()
ReDim bytContent(iLength) 'byte array, set to file size

'strip the path off the filename
i = InStrRev(File1.PostedFile.FileName.Trim, "\")
If i = 0 Then
sFileName = File1.PostedFile.FileName.Trim
Else
sFileName = Right(File1.PostedFile.FileName.Trim,
Len(File1.PostedFile.FileName.Trim) - i)
End If

File1.PostedFile.InputStream.Read(bytContent, 0, iLength)
If attachment_description.text = "" Then
lblError.text = "You must give the file a description"
Else
If iLength > 1300000 Then
lblError.text = "Your file must be under 1 Meg"
Else
Try
cmdInsertAttachment.Parameters.Add( "@Attachment_FileName",
sFileName )
cmdInsertAttachment.Parameters.Add( "@Attachment_FileSize",
iLength )
cmdInsertAttachment.Parameters.Add( "@Attachment_FileData",
bytContent )
cmdInsertAttachment.Parameters.Add( "@Attachment_ContentType",
sContentType )
cmdInsertAttachment.Parameters.Add( "@Attachment_Description",
attachment_description.text )
cmdInsertAttachment.Parameters.Add( "@incident_number",
viewstate("intMaxIncidentNumber") )
cmdInsertAttachment.ExecuteNonQuery()
Catch ex As Exception
'Handle your database error here
conMatt_I3.Close()
End Try
End If
End If

End If
Nov 18 '05 #1
2 1420
Hi Matt,

It looks like you're not sending out the data the right way for format. When
I've pushed out pdfs, I've used this code:

Dim byteTemp As Byte()
Dim strFileNameToDisplay As String
strFileNameToDisplay = "Documentname.pdf"
' Put the document bytes into a byte array
byteTemp = 'Get the bytes from the database here
Response.Buffer = True
Response.Clear()
Response.ClearContent()
Response.ClearHeaders()
Response.ContentType = "application/pdf"
Response.AddHeader("Content-Disposition", _
"attachment;filename=" & strFileNameToDisplay)
Response.BinaryWrite(byteTemp)
Response.End()

You'll want to check with this article on using BLOB data in SQL server:

http://support.microsoft.com/default...b;en-us;326502

Let us know what works?

Ken
Microsoft MVP [ASP.NET]
"Matt Mercer" <ma******@bellsouth.net> wrote in message
news:37**************************@posting.google.c om...
Hi all,

Well, thanks to some smart/helpful people here my upload and download
section of my website works great! I am allowing users to upload
directly into the SQL database but files cannot exceed 1 Meg (actually
1.2M but I tell them 1 meg) and a max of five uploads per incident (it
is a security incident reporting system)

I do not want to redesign this now that it is working, but I cannot
download pdfs! Acrobat tells me the file is corrupt (reader version
6). This happens if I open or save to disk and open later. Every other
file type I have tried works great.

As users upload attachments, a datagrid appears with the option to
view or delete attachments. Below is the code to view (which works for
all but pdf) and the next sub is the upload routine (might be
important, eh). Forgive my coding...I am new and self-taught as you
will undoubtedly notice :) Thanks in advance.

Sub dgrdAttachment_click (sender As Object, e as
DataGridCommandEventArgs)
Dim conMatt_I3 As SqlConnection
conMatt_I3 = New SqlConnection(
"Server=localhost;uid=sa;pwd=sa;database=Matt_ I3" )

conMatt_I3.Open()

If e.commandname="View" Then

Dim buttonColumn as TableCell = e.Item.Cells(0)
viewstate ("attachment_id") = buttonColumn.text

Dim cmdLoadAttachment As SqlCommand
Dim dtrLoadAttachment As SqlDataReader

cmdLoadAttachment = New SqlCommand ("SELECT * FROM attachment WHERE
attachment_id='" & viewstate ("attachment_id") & "'", conMatt_I3)
dtrLoadAttachment = cmdLoadAttachment.ExecuteReader()
dtrLoadAttachment.Read()

Response.ContentType =
dtrLoadAttachment("Attachment_ContentType").ToStri ng
Response.Outputstream.Write(Ctype(dtrLoadAttachmen t("Attachment_FileData"),
Byte() ), 0, Cint(dtrLoadAttachment("Attachment_FileSize")))
Response.AddHeader("Content-Disposition", "attachment;filename=" +
dtrLoadAttachment("Attachment_FileName").ToString( ))

End If
-----------------------------------------------------------------

Sub attachmentSubmit_Click ( s As Object, e As EventArgs )
Dim conMatt_I3 As SqlConnection
Dim cmdCheckFiveAttachments As SqlCommand
Dim cmdInsertAttachment As SqlCommand
Dim intCheckFiveAttachments

conMatt_I3 = New SqlConnection(
"Server=localhost;uid=sa;pwd=sa;database=Matt_ I3" )
conMatt_I3.Open()

cmdCheckFiveAttachments = New SqlCommand ("SELECT Count(*) FROM
attachment WHERE incident_number='" & viewstate
("intMaxIncidentNumber") & "'", conMatt_I3)

intCheckFiveAttachments = cmdCheckFiveAttachments.ExecuteScalar()
If intCheckFiveAttachments = 5 Then
lblError.text = "Only five attachments are allowed per Incident.
Please delete one and try again."
Else

cmdInsertAttachment = New SqlCommand ("Insert Attachment
(attachment_filename, attachment_description, incident_number,
attachment_filesize, attachment_filedata, attachment_contenttype)
Values ( @attachment_filename, @attachment_description,
@incident_number, @attachment_filesize, @attachment_filedata,
@attachment_contenttype )", conMatt_I3)
Dim iLength As Integer = CType(File1.PostedFile.InputStream.Length,
Integer)
If iLength = 0 Then Exit Sub 'not a valid file
Dim sContentType As String = File1.PostedFile.ContentType
Dim sFileName As String, i As Integer
Dim bytContent As Byte()
ReDim bytContent(iLength) 'byte array, set to file size

'strip the path off the filename
i = InStrRev(File1.PostedFile.FileName.Trim, "\")
If i = 0 Then
sFileName = File1.PostedFile.FileName.Trim
Else
sFileName = Right(File1.PostedFile.FileName.Trim,
Len(File1.PostedFile.FileName.Trim) - i)
End If

File1.PostedFile.InputStream.Read(bytContent, 0, iLength)
If attachment_description.text = "" Then
lblError.text = "You must give the file a description"
Else
If iLength > 1300000 Then
lblError.text = "Your file must be under 1 Meg"
Else
Try
cmdInsertAttachment.Parameters.Add( "@Attachment_FileName",
sFileName )
cmdInsertAttachment.Parameters.Add( "@Attachment_FileSize",
iLength )
cmdInsertAttachment.Parameters.Add( "@Attachment_FileData",
bytContent )
cmdInsertAttachment.Parameters.Add( "@Attachment_ContentType",
sContentType )
cmdInsertAttachment.Parameters.Add( "@Attachment_Description",
attachment_description.text )
cmdInsertAttachment.Parameters.Add( "@incident_number",
viewstate("intMaxIncidentNumber") )
cmdInsertAttachment.ExecuteNonQuery()
Catch ex As Exception
'Handle your database error here
conMatt_I3.Close()
End Try
End If
End If

End If


Nov 18 '05 #2
Response.AddHeader("Content-Length",
dtrLoadAttachment("Attachment_FileSize").ToString( ))

Adding this line to my code before outputting the file has fixed it
for me. Weird.
Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: RJ Dake | last post by:
Having problems with uploaded Database and SharePoint sites. At least one of the errors is mentioned below. Sites do not allow access to DB entry or results. Email feedback is MOST appreciated!...
11
by: Taishi | last post by:
Andy, I am new to SQL. It was hard just to figure out how to install it. I know I am logged in as a Admin. in Windows. I have the little server box with the round circle on it in the bottom...
19
by: MLH | last post by:
I call the following Sub and Function in frmLaunch's OnOpen event code. I keep getting Property Not Found error for the AllowBypassKey setting. Failure point is line #30 in the Function (not the...
0
by: Rhon Stewart via DotNetMonster.com | last post by:
Hi please visit this link : http://www.eggheadcafe.com/articles/pfc/selfupdater.asp I followed all the steps for listed on the link , when I execute the application it it gives me the following...
0
by: TJ | last post by:
Hi, I've written code web-based uploading and downloading. Here is some code for it. For saving file into MS-SQL database, SaveFileIntoDB(HttpPostedFile file) { int fileLength =...
0
by: Mamatha | last post by:
Hi i have one ASP page,when i click a download button in that page it downloads a file from net,and the file size is nearly 70MB,so while at the time of downloading a file the asp page...
2
by: Tomas Martinez | last post by:
Hi there! I'm trying to download a file in my asp.net web, but when downloading it from a Firefox browser, instead of downloading the example.exe file, it's downloading example.exe.htm. My code...
1
by: Steve Ametjan | last post by:
I've been trying to get MySQL-python to install on Leopard for the past couple of days, and I keep running into relatively the same error. I'm hoping that someone on this list will be able to...
0
by: =?Utf-8?B?VmFuZXNzYQ==?= | last post by:
Hi all! I am having problems on installing .NET Framework 3.5 on some machines. In one machine the error is: WinVerifyTrust returned -2146762751 Wintrust not on machine Error: O arquivo...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.