A
AZNewsh
The code I am using to download blobs from an oracle database giving an
open or save option is given below, This code seems to work fine with
files of type .doc or graphics files but when I tried a pdf file I got
the following error when acrobat reader tried to open it: "There was an
error opening this document. The file is damaged and could not be
repaired".
This file was tested fine before being uploaded and also tested fine
when I did a save directly from Oracle using Toad, so the problem would
seem to be in how it is being downloaded???
Any thoughts?
Imports System.Data.OracleClient
Public Class view_file
Inherits spac_template
Private fileID As String
" Web Form Designer Generated Code "
Dim objdr As OracleDataReader
Dim objcnn As OracleConnection
Dim objcom As OracleCommand
fileID = Request.QueryString("fileID")
Try
objcnn = New
OracleConnection(Application("gConnString").ToString)
objcnn.Open()
objcom = New OracleCommand("SELECT FILEID " & _
"FROM TABLE " & _
"WHERE FILEID = " & fileID,
objcnn)
objdr = objcom.ExecuteReader
If objdr.HasRows Then
Blob_OpenOrSave(fileID)
Else
lblError.Text = "That file is no longer available in
the database"
End If
If Not IsNothing(objcnn) Then
If objcnn.State = ConnectionState.Open Then
objcnn.Close()
End If
End If
Catch ex As Exception
utilities.ErrorHandler(ex, lblError)
End Try
End Sub
Public Function Blob_OpenOrSave(ByVal strPkValue As String) As
String
Dim sqlBlob As String = ""
Dim cn As OracleConnection
Dim cmd As OracleCommand
Dim dr As OracleDataReader
Dim intBlobCol As Integer
Dim strErrorMsg As String = ""
Try
sqlBlob = _
"SELECT * " & _
"FROM TABLE " & _
"WHERE FILEID = " & strPkValue
cn = New
OracleConnection(Application("gConnString").ToString)
cmd = New OracleCommand(sqlBlob, cn)
cn.Open()
Try
dr = cmd.ExecuteReader()
Try
dr.Read()
intBlobCol = CType(dr.GetOrdinal("FILE_CONTENT"),
Integer)
Dim MyData(CType(dr.GetBytes(intBlobCol, 0,
Nothing, 0, Integer.MaxValue) - 1, Integer)) As Byte
dr.GetBytes(intBlobCol, 0, MyData, 0,
MyData.Length)
Response.Buffer = True
Response.AddHeader("Content-Disposition",
"attachment;filename=" & dr.Item("FILE_NAME").ToString)
Response.ContentType =
dr.Item("FILE_CONTENT_TYPE").ToString
Response.BinaryWrite(MyData)
Catch ex As Exception
strErrorMsg &= ex.ToString & "<p/>" & vbCrLf
Finally
dr.Close()
End Try
Catch ex As Exception
strErrorMsg &= ex.ToString & "<p/>" & vbCrLf
Finally
cn.Close()
End Try
Catch ex As Exception
strErrorMsg &= ex.ToString & "<p/>" & vbCrLf
End Try
Return strErrorMsg
End Function
End Class
open or save option is given below, This code seems to work fine with
files of type .doc or graphics files but when I tried a pdf file I got
the following error when acrobat reader tried to open it: "There was an
error opening this document. The file is damaged and could not be
repaired".
This file was tested fine before being uploaded and also tested fine
when I did a save directly from Oracle using Toad, so the problem would
seem to be in how it is being downloaded???
Any thoughts?
Imports System.Data.OracleClient
Public Class view_file
Inherits spac_template
Private fileID As String
" Web Form Designer Generated Code "
Dim objdr As OracleDataReader
Dim objcnn As OracleConnection
Dim objcom As OracleCommand
fileID = Request.QueryString("fileID")
Try
objcnn = New
OracleConnection(Application("gConnString").ToString)
objcnn.Open()
objcom = New OracleCommand("SELECT FILEID " & _
"FROM TABLE " & _
"WHERE FILEID = " & fileID,
objcnn)
objdr = objcom.ExecuteReader
If objdr.HasRows Then
Blob_OpenOrSave(fileID)
Else
lblError.Text = "That file is no longer available in
the database"
End If
If Not IsNothing(objcnn) Then
If objcnn.State = ConnectionState.Open Then
objcnn.Close()
End If
End If
Catch ex As Exception
utilities.ErrorHandler(ex, lblError)
End Try
End Sub
Public Function Blob_OpenOrSave(ByVal strPkValue As String) As
String
Dim sqlBlob As String = ""
Dim cn As OracleConnection
Dim cmd As OracleCommand
Dim dr As OracleDataReader
Dim intBlobCol As Integer
Dim strErrorMsg As String = ""
Try
sqlBlob = _
"SELECT * " & _
"FROM TABLE " & _
"WHERE FILEID = " & strPkValue
cn = New
OracleConnection(Application("gConnString").ToString)
cmd = New OracleCommand(sqlBlob, cn)
cn.Open()
Try
dr = cmd.ExecuteReader()
Try
dr.Read()
intBlobCol = CType(dr.GetOrdinal("FILE_CONTENT"),
Integer)
Dim MyData(CType(dr.GetBytes(intBlobCol, 0,
Nothing, 0, Integer.MaxValue) - 1, Integer)) As Byte
dr.GetBytes(intBlobCol, 0, MyData, 0,
MyData.Length)
Response.Buffer = True
Response.AddHeader("Content-Disposition",
"attachment;filename=" & dr.Item("FILE_NAME").ToString)
Response.ContentType =
dr.Item("FILE_CONTENT_TYPE").ToString
Response.BinaryWrite(MyData)
Catch ex As Exception
strErrorMsg &= ex.ToString & "<p/>" & vbCrLf
Finally
dr.Close()
End Try
Catch ex As Exception
strErrorMsg &= ex.ToString & "<p/>" & vbCrLf
Finally
cn.Close()
End Try
Catch ex As Exception
strErrorMsg &= ex.ToString & "<p/>" & vbCrLf
End Try
Return strErrorMsg
End Function
End Class