微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决导出多个 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 举报,一经查实,本站将立刻删除。