【问题标题】:Show word file from SQL to web page从 SQL 到网页显示 word 文件
【发布时间】:2016-12-28 19:45:33
【问题描述】:

我已将 .doc/.docx 和 pdf 文件以二进制格式存储在 SQL db 中。我想在网络浏览器中显示它们,我使用了以下代码。 .pdf 文件在这里没有问题,它在网页中正确显示。但是对于 .doc/.docx 有一个未格式化的数据显示在网页中。

DataSet ds = GetData(proID);
            Byte[] bytes = (Byte[])ds.Tables[0].Rows[0]["Resume"];
            Response.Buffer = true;
            Response.Charset ="";
            Response.Clear();
            Response.Cache.SetCacheability(HttpCacheability.NoCache);

            if (ds.Tables[0].Rows[0]["ContentType"].ToString() == ".doc" || ds.Tables[0].Rows[0]["ContentType"].ToString() == ".docx")
            {
                Response.ContentType = "application/vnd.ms-word";
            }
            else if (ds.Tables[0].Rows[0]["ContentType"].ToString() == ".pdf")
            {
                Response.ContentType = "application/pdf";
            }
            Response.BinaryWrite(bytes);
            Response.Flush();
            Response.End();

.doc/.docx 的 if 条件不起作用。如何将 .doc/.docx 文件的内容类型设为“text/html”。谁能帮我做这件事?

【问题讨论】:

  • 您是否只是尝试了明显的“应用程序/ms-word”
  • 是的。我尝试使用“application/ms-word”。但它会下载 .aspx 页面
  • 我用“Response.ContentType = “application/vnd.openxmlformats-officedocument.wordprocessingml.document”;”试过了。但它会从我想在浏览器/网页中显示的数据库中下载 word 文件

标签: c# sql .net file binary


【解决方案1】:

下面应该可以工作。

HttpContext.Current.Response.ContentType = "application/vnd.ms-word";

HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"

【讨论】:

  • 感谢您的回复.. 我试过了。它下载实际的word文件。但我需要在网页/浏览器中显示
  • 我已经更新了答案。无需使用内容配置
  • 是的。我尝试使用“application/vnd.ms-word”。但它会下载 .aspx 页面
  • 试试Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
  • @DheerajRoy 和 Nagaraj - 它下载实际的 word 文件。但我需要在网页/浏览器中显示..
猜你喜欢
  • 2017-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 2016-05-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多