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

mainrotor
mainrotor used Ask the Experts™
on
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

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Software Enthusiast
Commented:
Put the byte array into the Write method.

bw.Write(data)

Open in new window


https://msdn.microsoft.com/en-us/library/system.io.binarywriter(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/bx149a0k(v=vs.110).aspx

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial