Download pdf blob from oracle database

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
 
Joined
Nov 28, 2008
Messages
3
Reaction score
0
Hello there,

I am facing this same challenge at present, it is unfortunate that no body has been able to post a reply to this problem.

Please, if you have solved the problem, send me your solution, I will be very grateful.

My code is as below

using System;
using System.Collections.Generic;
using System.Web;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.OracleClient;
using System.IO;

/// <summary>
/// Summary description for WriteFile
/// </summary>
public class WriteFile
{

db MyDB = new db();
int ArraySize = 0;

public WriteFile()
{
//
// TODO: Add constructor logic here
//
}

public string WriteData(string ProposalID)
{
string sql = MyDB.SelectALL(ProposalID);
byte[] MyData = new byte[0];

OracleConnection conn = new OracleConnection(MyDB.MyConnectionString);
OracleDataAdapter da = new OracleDataAdapter(sql, conn);
OracleCommandBuilder MyCB = new OracleCommandBuilder(da);

DataSet ds = new DataSet();
da.Fill(ds, "theProposal");
DataRow myRow = ds.Tables["theProposal"].Rows[0];

if (myRow.IsNull("DIGISIGNEDIP")) { MyData = (byte[])myRow["PROPOSAL"]; }
else if (!myRow.IsNull("DIGISIGNEDIP")) { MyData = (byte[])myRow["DIGISIGNEDIP"]; }

ArraySize = new int();
ArraySize = MyData.GetUpperBound(0);

string MyPath = HttpContext.Current.Server.MapPath("~/DigitalSignedIP.pdf");

//Write the Proposal from the database (BLOB) to the file System
FileStream fsWrite = new FileStream(MyPath, FileMode.Create, FileAccess.Write);
//This requires that Write access be granted to this application on the Host server.
fsWrite.Write(MyData, 0, ArraySize);
fsWrite.Close(); //close the stream
fsWrite = null;

closeClosables(MyCB, ds, da, conn);
return MyPath;
}


public void closeClosables(OracleCommandBuilder MyCB, DataSet ds, OracleDataAdapter da, OracleConnection conn)
{
MyCB = null;
ds = null;
da = null;

conn.Close();
conn = null;
}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top