I am a newbie to the world of ASP.NET and I need some help. I have an upload page and a download page for inserting and retrieving files to a SQL database. The upload seems to be working, but when I download the files from the database I'm getting garbage in the files. Will someone please take a look and see if you can tell me what I've done wrong?
Thanks in advance!
UPLOAD:
<HTML>
<BODY>
<%
Set Upload = Server.CreateObject("Persits.Upload")
' we use memory uploads, so we must limit file size
Upload.SetMaxSize 100000, True
' Save to memory. Path parameter is omitted
Count = Upload.Save
' Obtain file object
Set File = Upload.Files("THEFILE")
If Not File Is Nothing Then
' Build ADO connection string
Connect = "Provider='SQLOLEDB' ;Data Source='DDD42\D1' ; Packet Size=32767;Initial Catalog='WebApps_DataCenter'; User ID='webappsdc'; Password='webappsdc&&2007'"
' Use ADO Recordset object
Set rs = Server.CreateObject("adodb.recordset")
' Optional: check whether this file already exists
filename = File.FileName
rs.Open "SELECT * from WebAppsDocuments WHERE filename='" & filename & "'", Connect, 2, 3
If Not rs.EOF Then
Response.Write "This file already exists in the database."
Response.End
End If
rs.Close
' Reopen recordset to insert file
rs.Open "WebAppsDocuments", Connect, 2, 3
rs.AddNew
rs("WebAppsDoc").value = File.Binary
rs("WebAppsDocCategoryID").value = Upload.Form("category")
rs("WebAppsDocsDesc").value = Upload.Form("DESCR")
rs("file_ext").value = right(trim(File.FileName),4)
rs("filename").value = File.FileName
rs("filesize").value = File.Size
rs.Update
Response.Write "File saved."
Else
Response.Write "File not selected."
End If
set rs = nothing
set Upload = nothing
%>
</BODY>
</HTML>
DOWNLOAD:
<!-- #include file="global.asp"-->
<%
filename = request("id")
'response.write (id)
Set Conn=Server.CreateObject("ADODB.Connection")
Conn.CursorLocation = 3
Conn.Open session("conn")
Set filers = Server.CreateObject("ADODB.Recordset")
fileSQL = ""
fileSQL = fileSQL & "SELECT * FROM WebAppsDocuments WHERE id = " & Request("id")
fileSQL = fileSQL & ";"
'response.write "<p>Program sql="&fileSQL&"<br>"
filers.Open fileSQL,Conn
If filers("file_ext") = ".txt" then
Response.ContentType = "text/html"
elseif filers("file_ext") = ".xls" then
Response.ContentType = "application/x-msexcel"
elseif filers("file_ext") = ".pdf" then
Response.ContentType = "application/pdf"
elseif filers("file_ext") = ".doc" then
Response.ContentType = "application/msword"
end if
' let the browser know the file name
Response.AddHeader "Content-Disposition", "attachment;filename=" & Trim(filers("id"))
' let the browser know the file size
Response.AddHeader "Content-Length", filers("filesize")
Response.BinaryWrite filers("WebAppsDoc")
%>
Thanks in advance!
UPLOAD:
<HTML>
<BODY>
<%
Set Upload = Server.CreateObject("Persits.Upload")
' we use memory uploads, so we must limit file size
Upload.SetMaxSize 100000, True
' Save to memory. Path parameter is omitted
Count = Upload.Save
' Obtain file object
Set File = Upload.Files("THEFILE")
If Not File Is Nothing Then
' Build ADO connection string
Connect = "Provider='SQLOLEDB' ;Data Source='DDD42\D1' ; Packet Size=32767;Initial Catalog='WebApps_DataCenter'; User ID='webappsdc'; Password='webappsdc&&2007'"
' Use ADO Recordset object
Set rs = Server.CreateObject("adodb.recordset")
' Optional: check whether this file already exists
filename = File.FileName
rs.Open "SELECT * from WebAppsDocuments WHERE filename='" & filename & "'", Connect, 2, 3
If Not rs.EOF Then
Response.Write "This file already exists in the database."
Response.End
End If
rs.Close
' Reopen recordset to insert file
rs.Open "WebAppsDocuments", Connect, 2, 3
rs.AddNew
rs("WebAppsDoc").value = File.Binary
rs("WebAppsDocCategoryID").value = Upload.Form("category")
rs("WebAppsDocsDesc").value = Upload.Form("DESCR")
rs("file_ext").value = right(trim(File.FileName),4)
rs("filename").value = File.FileName
rs("filesize").value = File.Size
rs.Update
Response.Write "File saved."
Else
Response.Write "File not selected."
End If
set rs = nothing
set Upload = nothing
%>
</BODY>
</HTML>
DOWNLOAD:
<!-- #include file="global.asp"-->
<%
filename = request("id")
'response.write (id)
Set Conn=Server.CreateObject("ADODB.Connection")
Conn.CursorLocation = 3
Conn.Open session("conn")
Set filers = Server.CreateObject("ADODB.Recordset")
fileSQL = ""
fileSQL = fileSQL & "SELECT * FROM WebAppsDocuments WHERE id = " & Request("id")
fileSQL = fileSQL & ";"
'response.write "<p>Program sql="&fileSQL&"<br>"
filers.Open fileSQL,Conn
If filers("file_ext") = ".txt" then
Response.ContentType = "text/html"
elseif filers("file_ext") = ".xls" then
Response.ContentType = "application/x-msexcel"
elseif filers("file_ext") = ".pdf" then
Response.ContentType = "application/pdf"
elseif filers("file_ext") = ".doc" then
Response.ContentType = "application/msword"
end if
' let the browser know the file name
Response.AddHeader "Content-Disposition", "attachment;filename=" & Trim(filers("id"))
' let the browser know the file size
Response.AddHeader "Content-Length", filers("filesize")
Response.BinaryWrite filers("WebAppsDoc")
%>