F
flutophilus
I have a routine that generates a file (actually a pdf) from a database blob
object. It has been working for several years on a variety of servers and
PC's. Now we have one server on which the routine generates empty ( zero
byte) files. This server is running Windows 2003 R2 SP2 but we have another
server with the same configuration which runs exactly the same release and
works fine. I've tried writing the files to different locations including a
hard-code folder on the C: drive and the Application.CommonAppDataPath but it
makes no difference. I've tried runnin as loca machine\administrator but it
doesn;t work.
Here's the code (dbhelper is a standard function that returns an open
connection object)
Public Function FetchPDFtoFile(ByVal booTestMode As Boolean, ByVal strRecno
As String, ByVal strTempPath As String) As String
' Read the specified PDF and create a data file
Dim dbHelper As New dbHelper
' open database connection
Dim dcConnection As OdbcConnection =
dbHelper.GetODBCConnection(booTestMode)
Using dcConnection
' set up the query string depending on user-supplied data
Dim strCommand As String = "SELECT pdf FROM pdfdocument WHERE
recno = " & strRecno
' read data into the DataReader. NB Sequential Access parm
Dim cmCommand As New OdbcCommand(strCommand, dcConnection)
Dim drReaderPDF As OdbcDataReader =
cmCommand.ExecuteReader(CommandBehavior.SequentialAccess)
If drReaderPDF.Read() Then
' create random temp file name
Dim dtNow As New Date
dtNow = Now()
Dim intMilliSec As Integer = dtNow.Millisecond()
Dim intSec As Integer = dtNow.Second
Dim randObj As New Random(intMilliSec + intSec)
Dim strPdfFileName As String = "doc" & randObj.Next.ToString
& ".pdf"
Directory.CreateDirectory(strTempPath)
Dim strPdfFullFileName As String = strTempPath &
strPdfFileName
'NB these temp files should be removed in the global section
when app shuts
' down but just in case ...
File.Delete(strPdfFullFileName)
' Create a filestream to write the output to the temp file.
Dim fs As New FileStream(strPdfFullFileName,
FileMode.OpenOrCreate, FileAccess.Write)
Dim bw As New BinaryWriter(fs)
Dim bufferSize As Integer = 1000 ' The size of the BLOB
buffer.
Dim outbyte(bufferSize - 1) As Byte ' The BLOB byte()
buffer to be filled by GetBytes.
Dim retval As Long ' The bytes returned
from GetBytes.
Dim startIndex As Long ' The starting position
in the BLOB output.
' Reset the starting byte for a new BLOB.
startIndex = 0
' Read bytes into outbyte() and retain the number of bytes
returned.
retval = drReaderPDF.GetBytes(0, startIndex, outbyte, 0,
bufferSize)
' Continue reading and writing while there are bytes beyond
the size of the buffer.
Do While retval = bufferSize
bw.Write(outbyte)
bw.Flush()
' Reposition the start index to the end of the last
buffer and fill the buffer.
startIndex += bufferSize
retval = drReaderPDF.GetBytes(0, startIndex, outbyte, 0,
bufferSize)
Loop
If retval > 0 Then
' Write the remaining buffer.
bw.Write(outbyte, 0, CInt(retval - 1))
bw.Flush()
End If
' Close the output file.
bw.Close()
fs.Close()
dbHelper.CloseODBC(dcConnection)
Return strPdfFullFileName
Else
dbHelper.CloseODBC(dcConnection)
Return Nothing
End If
End Using
End Function
object. It has been working for several years on a variety of servers and
PC's. Now we have one server on which the routine generates empty ( zero
byte) files. This server is running Windows 2003 R2 SP2 but we have another
server with the same configuration which runs exactly the same release and
works fine. I've tried writing the files to different locations including a
hard-code folder on the C: drive and the Application.CommonAppDataPath but it
makes no difference. I've tried runnin as loca machine\administrator but it
doesn;t work.
Here's the code (dbhelper is a standard function that returns an open
connection object)
Public Function FetchPDFtoFile(ByVal booTestMode As Boolean, ByVal strRecno
As String, ByVal strTempPath As String) As String
' Read the specified PDF and create a data file
Dim dbHelper As New dbHelper
' open database connection
Dim dcConnection As OdbcConnection =
dbHelper.GetODBCConnection(booTestMode)
Using dcConnection
' set up the query string depending on user-supplied data
Dim strCommand As String = "SELECT pdf FROM pdfdocument WHERE
recno = " & strRecno
' read data into the DataReader. NB Sequential Access parm
Dim cmCommand As New OdbcCommand(strCommand, dcConnection)
Dim drReaderPDF As OdbcDataReader =
cmCommand.ExecuteReader(CommandBehavior.SequentialAccess)
If drReaderPDF.Read() Then
' create random temp file name
Dim dtNow As New Date
dtNow = Now()
Dim intMilliSec As Integer = dtNow.Millisecond()
Dim intSec As Integer = dtNow.Second
Dim randObj As New Random(intMilliSec + intSec)
Dim strPdfFileName As String = "doc" & randObj.Next.ToString
& ".pdf"
Directory.CreateDirectory(strTempPath)
Dim strPdfFullFileName As String = strTempPath &
strPdfFileName
'NB these temp files should be removed in the global section
when app shuts
' down but just in case ...
File.Delete(strPdfFullFileName)
' Create a filestream to write the output to the temp file.
Dim fs As New FileStream(strPdfFullFileName,
FileMode.OpenOrCreate, FileAccess.Write)
Dim bw As New BinaryWriter(fs)
Dim bufferSize As Integer = 1000 ' The size of the BLOB
buffer.
Dim outbyte(bufferSize - 1) As Byte ' The BLOB byte()
buffer to be filled by GetBytes.
Dim retval As Long ' The bytes returned
from GetBytes.
Dim startIndex As Long ' The starting position
in the BLOB output.
' Reset the starting byte for a new BLOB.
startIndex = 0
' Read bytes into outbyte() and retain the number of bytes
returned.
retval = drReaderPDF.GetBytes(0, startIndex, outbyte, 0,
bufferSize)
' Continue reading and writing while there are bytes beyond
the size of the buffer.
Do While retval = bufferSize
bw.Write(outbyte)
bw.Flush()
' Reposition the start index to the end of the last
buffer and fill the buffer.
startIndex += bufferSize
retval = drReaderPDF.GetBytes(0, startIndex, outbyte, 0,
bufferSize)
Loop
If retval > 0 Then
' Write the remaining buffer.
bw.Write(outbyte, 0, CInt(retval - 1))
bw.Flush()
End If
' Close the output file.
bw.Close()
fs.Close()
dbHelper.CloseODBC(dcConnection)
Return strPdfFullFileName
Else
dbHelper.CloseODBC(dcConnection)
Return Nothing
End If
End Using
End Function