导出多个 blob 记录并保存到磁盘

问题描述

我在 sql 中有一个表存储 blob 数据 (varbinary(max)),我需要将其作为单独的文件导出到目录中。下面的代码 I 选择所有记录并创建目录并存储文件。但是,所有文件都是零字节。我需要帮助弄清楚如何在保存文件时将字节数据写入文件

文件数据库中存储为:

• ClientID int • 文件名 nvarchar(250)
文件扩展名 nvarchar(50)
• ContentType nvarchar(200)
• 数据变量(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()

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)