Link to home
Create AccountLog in
Avatar of mainrotor
mainrotor

asked on

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

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
ASKER CERTIFIED SOLUTION
Avatar of Avodah
Avodah
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account