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

本文关键字:pdf word 文件 网页 数据库 显示 | 更新日期: 2023-09-27 18:30:05

我的db-sql中有word,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文件,它会显示文件,但对于内容类型为单词"application/vnd.ms-word"的文件,它不起作用。我想更改ViewProfile页面中<object>中的"类型"。

有人能帮我做这个吗?

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

试试这个

Word.docx

application/vnd.openxmlformats-officedocument.wordprocessingml.document

而不是application/vnd.ms-word=>(.doc