Click here to Skip to main content
Click here to Skip to main content
Go to top

Download binary data from database using a Windows application

, 14 Mar 2005
Rate this:
Please Sign up or sign in to vote.
Function to download and open a file saved as IMAGE data in an SQL database.

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...

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

SUNILT
Architect
United States United States
No Biography provided

Comments and Discussions

 
QuestionHow to get TempWorkDir for my application Pinmemberskajaykumar7-May-07 1:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140905.1 | Last Updated 14 Mar 2005
Article Copyright 2005 by SUNILT
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid