从字节[]转换为文件

时间:2018-08-22 00:49:06

标签: sql-server vb.net file-conversion

我使用以下函数将文件转换为Byte()

Public Function FileToByteArray(ByVal _FileName As String) As Byte()
    Dim _Buffer() As Byte = Nothing

    Try
        ' Open file for reading
        Dim _FileStream As New System.IO.FileStream(_FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read)

        ' attach filestream to binary reader
        Dim _BinaryReader As New System.IO.BinaryReader(_FileStream)

        ' get total byte length of the file
        Dim _TotalBytes As Long = New System.IO.FileInfo(_FileName).Length

        ' read entire file into buffer
        _Buffer = _BinaryReader.ReadBytes(CInt(Fix(_TotalBytes)))

        ' close file reader
        _FileStream.Close()
        _FileStream.Dispose()
        _BinaryReader.Close()
    Catch _Exception As Exception
        ' Error
        Console.WriteLine("Exception caught in process: {0}", _Exception.ToString())
    End Try

    Return _Buffer
End Function

我使用此函数将文件("C:\sample.xlsx"保存为SqlDbType.Image到数据库(我使用Microsoft SQL Server)

下面是我用来从db获取值并尝试再次将其转换为文件的代码。

Dim FileInBinary() As Byte
Dim CurrentRS As Recordset

'Select
SQLString = "SELECT FileInBinary from table where ID=1"

'Get value to a recordSet
CurrentRS = ServerRunSQL_Return(SQLString)

'SaveValue to Dim
FileInBinary = CurrentRS.Fields(0).Value

'Try to convert
My.Computer.FileSystem.WriteAllBytes("C:\sample_new.xlsx", FileInBinary, True)

我也尝试过:

File.WriteAllBytes("C:\sample_new.xlsx", FileInBinary)

如何选择此值并将其重新转换为文件("C:\sample_new.xlsx")?

1 个答案:

答案 0 :(得分:1)

将函数FileToByteArray替换为Options +FollowSymLinks -MultiViews AddType application/rdf+xml .rdf AddType application/rdf+xml .owl AddType text/turtle .ttl AddType application/n-triples .n3 AddType application/ld+json .json RewriteEngine On RewriteCond %{REQUEST_FILENAME} -f [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule .* - [L] RewriteCond %{HTTP_ACCEPT} !application/rdf\+xml.*(text/html|application/xhtml\+xml) RewriteCond %{HTTP_ACCEPT} text/html [OR] RewriteCond %{HTTP_ACCEPT} application/xhtml\+xml [OR] RewriteCond %{HTTP_USER_AGENT} ^Mozilla/.* RewriteRule ^v0\.1/?$ https://miranda-zhang.github.io/cloud-computing-schema/v0.1/index.htm [R=308,NE,L] RewriteRule ^v1\.0/?$ https://miranda-zhang.github.io/cloud-computing-schema/v1.0/index-en.html [R=308,NE,L] RewriteCond %{HTTP_ACCEPT} application/ld\+json RewriteRule ^v1\.0$ https://miranda-zhang.github.io/cloud-computing-schema/v1.0/ontology.json [R=308,L] RewriteCond %{HTTP_ACCEPT} \*/\* [OR] RewriteCond %{HTTP_ACCEPT} application/rdf\+xml RewriteRule ^v0\.1$ https://miranda-zhang.github.io/cloud-computing-schema/v0.1/ontology/cocoon.rdf [R=308,L] RewriteRule ^v1\.0$ https://miranda-zhang.github.io/cloud-computing-schema/v1.0/ontology.xml [R=308,L] RewriteCond %{HTTP_ACCEPT} application/n-triples RewriteRule ^v1\.0$ https://miranda-zhang.github.io/cloud-computing-schema/v1.0/ontology.nt [R=308,L] RewriteCond %{HTTP_ACCEPT} text/turtle [OR] RewriteCond %{HTTP_ACCEPT} text/\* [OR] RewriteCond %{HTTP_ACCEPT} \*/turtle RewriteRule ^v0\.1$ https://miranda-zhang.github.io/cloud-computing-schema/v0.1/ontology/cocoon.ttl [R=308,L] RewriteRule ^v1\.0$ https://miranda-zhang.github.io/cloud-computing-schema/v1.0/ontology.ttl [R=308,L] RewriteCond %{HTTP_ACCEPT} .+ RewriteRule ^$ https://miranda-zhang.github.io/cloud-computing-schema/v1.0/406.html [R=406,L] # Default response RewriteRule ^$ https://miranda-zhang.github.io/cloud-computing-schema/v1.0/ontology.xml [R=308,L]

下面是从db获取值并将其再次转换为文件的代码。

File.ReadAllBytes("C:\sample.xlsx")