Introduction
I was working on a web application for uploading contractor employee (35,000+) information over the internet, that included documents, images etc.. Internally, the organization would be using a VB.NET Windows application to view, verify and issue security clearance to these submissions. The ASP.NET part for Download/Upload to and from an SQL 2000 backend (table with a column of data type IMAGE
), was pretty straightforward. Thanks to MS IE/browser functionality.
However with the Windows application, it was a totally different approach. It took me a bit of time before I was able to cook up something. I don't know if this is the best approach, however, I will list the source code for the benefit of anyone stuck in a similar situation.
Slap the below code into your application:
Friend Function OpenDownloadDoc(ByVal fname As String, _
ByVal bytBLOBData() As Byte) As Boolean
Dim binwrt As BinaryWriter
Try
With binwrt
Dim flstrm As FileStream = _
New FileStream(TempWorkDir & "\" & fname, _
FileMode.OpenOrCreate, FileAccess.Write)
binwrt = New BinaryWriter(flstrm)
.Write(bytBLOBData)
.Flush()
.Close()
flstrm.Close()
System.Diagnostics.Process.Start(TempWorkDir & "\" & fname)
End With
Return True
Catch ex As Exception
MsgBox(ex.Message)
Return False
End Try
End Function
The function parameters are a file name (with extension) and the binary data in a Byte()
.
System.Diagnostics.Process.Start(TempWorkDir & "\" & fname)
The above line forces the file to be opened using the appropriate application registered for use with the file extension. TempWorkDir
is a temporary folder (cache) assigned to the application.
Friend Sub FlushTempDir()
Dim fd As Directory
Dim fl As String
Dim xfl As File
For Each fl In fd.GetFiles(TempWorkDir)
xfl.Delete(fl)
Next
End Sub
Call the above function when the application quits, to clean the cache.
Simple? Hope it helps someone out there...
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.