Search code examples
sql-serverbinarydocxletter

Reading a docx (binary) from a SQL Server database and convert it into letter (docx)


OK I managed to upload the word DOCX into my SQL Server database into a varbinary (Max) column.

I can retrieve the DOCX from the database and covert it from varbinary back into an array and offer it as a download with:

    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();

Instead of downloading the document I would prefer to use it in variable so I exchange placeholders in it.

I tried to find a way to convert it to a string or so I can use it for docx eg.

 DocX letter = this.document();

Best option I saw so far was the filestream version

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;
}

But I couldn't convert the binary array bytes nor the memory stream into a variable docx would understand. Maybe I just looked for the wrong conversation. Can someone give me a hint please?

The field is called TD_DocContentType from the database. I can accept that I am weak on the conversion in this instance. I can't see what I am doing wrong. Need a new idea please. Kind Regards, Rene


Solution

  • I found the solution for my problem. Took a while and lot of more research to do. I tried to tackle the problem from the wrong angle.

    This solution reads the binary field from the database and writes it as a file to an internal web folder called doctemp.

    private void download(DataTable dt)

    {
        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);
    }