Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 从SQL Server数据库读取docx(二进制)并将其转换为字母(docx)_Sql Server_Binary_Docx_Letter - Fatal编程技术网

Sql server 从SQL Server数据库读取docx(二进制)并将其转换为字母(docx)

Sql server 从SQL Server数据库读取docx(二进制)并将其转换为字母(docx),sql-server,binary,docx,letter,Sql Server,Binary,Docx,Letter,好的,我设法将DOCX这个词上传到我的SQL Server数据库的varbinary(Max)列中 我可以从数据库中检索DOCX,并将其从varbinary转换回一个数组中,并提供下载: Byte[] bytes = (Byte[])dt.Rows[0]["TD_DocFile"]; Response.Buffer = true; Response.Charset = ""; Response.Cache.SetCacheability(HttpCacheabil

好的,我设法将DOCX这个词上传到我的SQL Server数据库的
varbinary(Max)
列中

我可以从数据库中检索DOCX,并将其从varbinary转换回一个数组中,并提供下载:

    Byte[] bytes = (Byte[])dt.Rows[0]["TD_DocFile"];
    Response.Buffer = true;
    Response.Charset = "";
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.ContentType = dt.Rows[0]["TD_DocContentType"].ToString();
    Response.AddHeader("content-disposition", "attachment;filename="
    + dt.Rows[0]["TD_DocTitle"].ToString());
    Response.BinaryWrite(bytes);
    Response.Flush();
    Response.End();
与其下载文档,我更喜欢在变量中使用它,所以我在其中交换占位符

我试图找到一种方法将其转换为字符串,或者我可以将其用于docx例如

 DocX letter = this.document();
到目前为止,我看到的最佳选择是filestream版本

public static MemoryStream databaseFileRead(string varID) {
    MemoryStream memoryStream = new MemoryStream();
    using (var varConnection = Locale.sqlConnectOneTime(Locale.sqlDataConnectionDetails))
    using (var sqlQuery = new SqlCommand(@"SELECT [RaportPlik] FROM [dbo].[Raporty] WHERE [RaportID] = @varID", varConnection)) {
        sqlQuery.Parameters.AddWithValue("@varID", varID);
        using (var sqlQueryResult = sqlQuery.ExecuteReader())
            if (sqlQueryResult != null) {
                sqlQueryResult.Read();
                var blob = new Byte[(sqlQueryResult.GetBytes(0, 0, null, 0, int.MaxValue))];
                sqlQueryResult.GetBytes(0, 0, blob, 0, blob.Length);
                //using (var fs = new MemoryStream(memoryStream, FileMode.Create, FileAccess.Write)) {
                memoryStream.Write(blob, 0, blob.Length);
                //}
            }
    }
    return memoryStream;
}
但我无法将二进制数组字节或内存流转换为docx可以理解的变量。也许我只是找错了话题。有人能给我一个提示吗

该字段从数据库中被称为
TD_DocContentType
。我可以接受,在这种情况下,我在转换方面很弱。我看不出我做错了什么。我需要一个新主意。 亲切问候,,
Rene

我找到了解决问题的办法。花了一段时间,还有很多研究要做。我试图从错误的角度解决这个问题

此解决方案从数据库读取二进制字段,并将其作为文件写入名为doctemp的内部web文件夹

私有void下载(数据表dt)


谢谢你,Marc_s-刚刚又读了一遍,你读得更快了。感谢您的更改。您是否考虑过进行基线调试?例如,您从数据库中提取的数据是否与您输入的数据相同(例如,长度、前8个字节和后8个字节)?谢谢TomTom,是的,正如我所说,我可以从数据库中获取文档并下载它。我的问题是如何转换它,以便在程序中与docx一起使用。你在说什么?如果它是docx的二进制表示形式,则没有转换,只需将字节保存在以.docx.Ok结尾的正确文件名下,TomTom,这是我试图确定的。非常感谢。然后我需要将它保存在服务器上,而不是本地。现在看看这个选项。
{
    var physicalPath = Server.MapPath("~\\doctemp\\{0}");
    string outputFileName = string.Format(physicalPath, dt.Rows[0]["TD_DocTitle"]);
    filename = outputFileName;

    Byte[] bytes = (Byte[])dt.Rows[0]["TD_DocFile"];
    File.WriteAllBytes(outputFileName, bytes);
}