简体   繁体   中英

Open Word file stored in SQL Server

We successfully add word documents in our SQL Server (varbinary column) using some web forms, then, we want some of our staff to be able to download them.

We have some code to display it, but sometimes it does display (in my local environment), sometimes it does not, but then, when it goes live to Production. So, we just want to download it, it doesn't matter if it opens or if it does not (in case you don't have Word installed), we just want to have it downloaded, something like the "Save as..." function. Because in my local environment it works well, but when it goes live to a IIS server, we are not able to retrieve the file (I am guessing, it is because it opens WORD automatically).

Here is the code to retrieve it.

    public string showDoc(int id, int numRef)
    {
        string fileName = Path.GetTempFileName() + ".docx";
        Db.Open();
        string cuerito = "select doc from tbl_references where [userId]=@id and [refNum]=@numRef";
        SqlCommand command = new SqlCommand(cuerito, base.Db);
        command.Parameters.AddWithValue("@id", id);
        command.Parameters.AddWithValue("@numRef", numRef);
        using (SqlDataReader dr = command.ExecuteReader())
            {
                while (dr.Read())
                {
                    int size = 1024 * 1024;
                    byte[] buffer = new byte[size];
                    int readBytes = 0;
                    int index = 0;

                    using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
                    {
                        while ((readBytes = (int)dr.GetBytes(0, index, buffer, 0, size)) > 0)
                        {
                            fs.Write(buffer, 0, readBytes);
                            index += readBytes;
                        }
                    }

            }
        }
        Db.Close();
        return fileName;
    }

Is there a way in C# just to do this? Not display but only download it from SQL Server?

EB.

All the code above does is read some data from a binary field and write it to a stream, which gets written to the disk on the server. There's no need to save the file to disk before you offer it for download, you can simply stream the data directly to the client - there are tons of examples and previous questions containing working examples of this process if you google it. They will work with any file, none of this is specific to Word files. And none of it relates to interop in any way.

BTW whether the file downloads directly to the user's disk or is opened up (saved in a temp folder on the user's machine) is largely down to the config of the user's machine, which browser they use, whether the Word plugin is installed in that browser, their general download settings. You can set HTTP headers (again google it) which hint to the browser to just save the file instead of trying to open it, but ultimately it's in the control of the browser, not the server.

Here's one reasonably simple example of sending binary data from a database table as a file download: https://ygtechme.wordpress.com/2012/08/27/downloading-file-from-database-using-c-asp-net/ . There are dozens more similar ones with slight variations on the theme available online. But this is the general idea.

您发布的代码已经可以满足您的要求。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM