We help IT Professionals succeed at work.

How can I write a byte array to a file using a SaveFileDialog

237 Views
Last Modified: 2017-12-02
Good Morning Experts,
I need help Reading and Saving files and images stored as varbinary data in my database using SaveFileDialog in my VB.Net application.

I am able retrieve the data column from my SQL Server DB.  Now that I have the data, I want to save the data to a selected file path via SaveFileDialog.  How do I do this?

The varbinary data could be a .pdf, .tif, .jpg, or .docx.

This is the code I have so far.  What should I put inside the parenthesis of bw.Write()
 
    Private Sub btnRetrieve_Click(sender As System.Object, e As System.EventArgs) Handles btnRetrieve.Click
        Dim dta As New TestDBDataSetTableAdapters.sp_SelectFileTableAdapter
        Dim dt As TestDBDataSet.sp_SelectFileDataTable = dta.GetData(6)
        Dim file As String = "D:\temp\test.docx"

        Dim dr As DataRow
        For Each dr In dt.Rows
            WriteWordDoc(file, dr("FileData"))
        Next
    End Sub

    Private Sub WriteWordDoc(filename As String, data As Byte())
        Dim fs As New System.IO.FileStream(filename, IO.FileMode.Create)
        Dim bw As New System.IO.BinaryWriter(fs)
        bw.Write()
        bw.Close()
        fs.Close()
    End Sub

Open in new window


Thank you,
mrotor
Comment
Watch Question

Software Enthusiast
CERTIFIED EXPERT
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION