0

I have a table in sql storing blob data (varbinary(max)) that I need to export as separate file in directories. The code I below selects all of the records and creates directories and stores the file. However, all the files are zero bytes. I need help figuring out how to write the byte data to the file as it is being saved.

The files are stored in the db as:

• ClientID int • FileName nvarchar(250)
• FileExtension nvarchar(50)
• ContentType nvarchar(200)
• Data varbinary(MAX)

'if directory does not exist...
                    Directory.CreateDirectory(Server.MapPath(pathToCreate))

                    Dim id As Integer = reader.Item("ClientID")
                    Dim bytes() As Byte
                    Dim filename As String
                    Dim mycon As String = ConfigurationManager.ConnectionStrings("Document_DB").ConnectionString
                    Dim con As New SqlConnection(mycon)
                    con.Open()
                    Dim query As String = "Select FileName, Data, ContentType from FileStore where ClientID=@ClientID"
                    Dim cmd As New SqlCommand(query)
                    cmd.Parameters.AddWithValue("@ClientID", id)
                    cmd.Connection = con
                    Dim sdr As SqlDataReader = cmd.ExecuteReader()
                    While sdr.Read()
                        bytes = CType(sdr("Data"), Byte())
                        filename = sdr("FileName").ToString()
                        Dim filePath As String = pathToCreate & "/" & filename

                        '**this will create the file in its path but with zero bytes
                        'But, I cannot figure out how to write bytes to the files as they are being saved.
                        File.Create(MapPath(filePath))


                        '***this code works if I want to save one file at a time in a web browser. But, I can't figure out how to get Response.BinaryWrite(bytes) to work above.
                        'Response.Clear()
                        'Response.Buffer = True
                        'Response.Charset = ""
                        'Response.Cache.SetCacheability(HttpCacheability.NoCache)
                        'Response.ContentType = ContentType
                        'Response.AppendHeader("Content-Disposition", "attachment; filename=" & filename)
                        'Response.BinaryWrite(bytes)
                        'Response.Flush()
                        'Response.End()

                    End While
                    sdr.Close()
                    con.Close()
rednelo
  • 3
  • 3
  • 1
    `File.WriteAllBytes` maybe? By the way you should dispose connection, command and reader with `Using` blocks – Charlieface Jul 21 '21 at 02:22
  • And don't use [addwithvalue](http://www.dbdelta.com/addwithvalue-is-evil/) – SMor Jul 21 '21 at 02:55
  • Charlieface- WriteAllBytes worked. I just changed File.Create(MapPath(filePath)) to File.WriteAllBytes(MapPath(filePath), bytes). – rednelo Jul 21 '21 at 12:41

0 Answers0