为什么不超链接打开该文件? [英] Why doesn't the hyperlink open the file?
问题描述
此前开发商已经把使用这个标签code和它变成了一个超链接在运行时间。
< ASP:标签ID =lbl_Attachment=服务器>
< / ASP:标签>lbl_Attachment.Text =< A HREF ='.. /上传/+
ds_row [附件]的ToString()+'。>中+
ds_row [附件]的ToString()+< / A>中。
但是,这是行不通的。所以我改变了code以下在新的浏览器标签页中打开的任何文件(图像/ PDF /字)和错误仍然存在:
hyperlinkAppDoc.NavigateUrl =
RESOLVEURL(../上传/+
。ds_row [附件]的ToString());
hyperlinkAppDoc.Target =_blank;
我能做些什么来解决这个问题? MIME类型是在IIS可用。
更新:
我尝试了不同的方法。然而,使用Server.Mappath
在本地驱动器,而不是wwwroot文件poiting。我怎么可以指向路径wwwroot文件夹?
字符串pdfPath =使用Server.Mappath(〜/ SomeFile.pdf的);
Web客户端的客户端=新的WebClient();
字节[]缓冲= client.DownloadData(pdfPath);
Response.ContentType =应用程序/ PDF
Response.AddHeader(内容长度,buffer.Length.ToString());
Response.BinaryWrite(缓冲液);
您也可以使用 asp.hyperlink
。就像下面
< ASP:超链接ID =hyperlink1NavigateUrl =< set_from_ code_behind>中文本=TEXT重定向=服务器/>
和从code设置NavigateUrl背后如下。
hyperlink1.NavigateUrl =上传/+ ds_row [附件]的ToString();
Earlier developer had put this code using a label and it was turned into a hyperlink during the run time.
<asp:Label ID="lbl_Attachment" runat="server">
</asp:Label>
lbl_Attachment.Text = "<a href='../Uploads/" +
ds_row["Attachment"].ToString() + "'>" +
ds_row["Attachment"].ToString() + "</a>";
But this is not working. So I changed the code to the following to open the any file (image/pdf/word) in a new browser tab and the error persists:
hyperlinkAppDoc.NavigateUrl =
ResolveUrl("../Uploads/" +
ds_row["Attachment"].ToString());
hyperlinkAppDoc.Target = "_blank";
What can I do to fix this issue? MIME types are available in the IIS.
UPDATE:
I am trying out a different approach. However the Server.MapPath
is poiting at local drive instead of wwwroot. How can I point the path to wwwroot folder?
string pdfPath = Server.MapPath("~/SomeFile.pdf");
WebClient client = new WebClient();
Byte[] buffer = client.DownloadData(pdfPath);
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", buffer.Length.ToString());
Response.BinaryWrite(buffer);
You could have used asp.hyperlink
. Like following
<asp:HyperLink id="hyperlink1" NavigateUrl="<set_from_code_behind>" Text="Text to redirect" runat="server"/>
And set NavigateUrl from code behind as following.
hyperlink1.NavigateUrl= "Uploads/" + ds_row["Attachment"].ToString();
这篇关于为什么不超链接打开该文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!