我在我的數據庫sql有word,pdf文件。我想在一個新頁面中顯示他們,同時點擊鏈接。在列表頁我用下面的代碼:從數據庫的網頁顯示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>
中更改「type」。
任何人都可以幫助我做到這一點?
您的Word文檔是.DOCX或一個.doc? – Alexis
.docx和分貝我已經將其contentType保存爲「應用程序/ vnd.ms字」 – KaviSuja