在数据库的网页中显示word / pdf文件

时间:2016-01-12 08:41:14

标签: c# .net pdf filestream

我在db sql中有单词,pdf文件。我想在点击链接的同时在新页面中显示它们。在列表页面中,我使用以下代码:

<asp:TemplateField HeaderText="Resume">        
                       <ItemStyle HorizontalAlign="Center" />                  
                    <ItemTemplate>
                        <a href ='<%#"ViewProfile.aspx?ProfileID="+DataBinder.Eval(Container.DataItem,"ProfileID") %>' id="hlprofile">View Profile</a>                  
                    </ItemTemplate>
                </asp:TemplateField>   

在ViewProfile.aspx.cs的页面加载中,我有以下内容:

 int id = int.Parse(Request.QueryString["ProfileID"]);
 string embed = "<object data=\"{0}{1}\" type=\"application/pdf\"  width=\"1100px\" height=\"700px\">";
                          embed += "If you are unable to view file, you can download from <a href = \"{0}{1}&download=1\">here</a>";
                          embed += " or download <a target = \"_blank\" href = \"http://get.adobe.com/reader/\">Adobe PDF Reader</a> to view the file.";
                          embed += "</object>";

                          ltEmbed.Text = string.Format(embed, ResolveUrl("~/FileView.ashx?ProfileID="), id);

并在FileView.ashx.cs中包含以下内容:

int proID = int.Parse(context.Request.QueryString["ProfileID"]);
            byte[] bytes;
            string fileName, contentType;

            DataTable ds = new DataTable();
              ds = objProfile.GetResume(proID);
            if (ds.Rows.Count == 1)
            {
                foreach (DataRow dRow in ds.Rows)
                {
                    bytes = (byte[])dRow["Resume"];
                    fileName = dRow["ResumeName"].ToString();
                    contentType = dRow["ContentType"].ToString();

                    context.Response.Buffer = true;
                    context.Response.Charset = "";
                    if (context.Request.QueryString["download"] == "1")
                    {
                        context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
                    }


                      if (contentType == "application/vnd.ms-word")
                        {
                            context.Response.ContentType = "application/vnd.ms-word";
                        }
                        else if (contentType == "application/pdf")
                          {
                              context.Response.ContentType = "application/pdf";
                             }
                      context.Response.Charset = "";
                      context.Response.Cache.SetCacheability(HttpCacheability.NoCache);

                    //context.Response.ContentType = "application/pdf";
                    context.Response.BinaryWrite(bytes);
                    context.Response.Flush();
                    context.Response.End();
                }
            }

对于PDF文件,它会显示文件,但内容类型为word&#39; application / vnd.ms-word&#39;它不工作。我认为要改变&#39;键入&#39;在<object>中的ViewProfile页面中。

任何人都可以帮我这样做吗?

1 个答案:

答案 0 :(得分:0)

试试这个

Word .docx

application/vnd.openxmlformats-officedocument.wordprocessingml.document

而不是application/vnd.ms-word =&gt; (的 .DOC