为什么超链接打不开文件?
Why doesn't the hyperlink open the file?
早期的开发人员使用标签放置此代码,并在 运行 时间将其变成超链接。
<asp:Label ID="lbl_Attachment" runat="server">
</asp:Label>
lbl_Attachment.Text = "<a href='../Uploads/" +
ds_row["Attachment"].ToString() + "'>" +
ds_row["Attachment"].ToString() + "</a>";
但这不起作用。所以我将代码更改为以下代码以在新的浏览器选项卡中打开任何文件 (image/pdf/word),但错误仍然存在:
hyperlinkAppDoc.NavigateUrl =
ResolveUrl("../Uploads/" +
ds_row["Attachment"].ToString());
hyperlinkAppDoc.Target = "_blank";
我该怎么做才能解决这个问题? MIME 类型在 IIS 中可用。
更新:
我正在尝试一种不同的方法。但是 Server.MapPath
指向本地驱动器而不是 wwwroot。如何指向 wwwroot 文件夹的路径?
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);
您本可以使用 asp.hyperlink
。喜欢关注
<asp:HyperLink id="hyperlink1" NavigateUrl="<set_from_code_behind>" Text="Text to redirect" runat="server"/>
并从后面的代码中设置 NavigateUrl,如下所示。
hyperlink1.NavigateUrl= "Uploads/" + ds_row["Attachment"].ToString();
根据您的情况,我建议您在网络配置文件的 <appSettings>
标记中添加一个键。
<appSettings>
<add key="WebsiteURL" value="http://localhost:1234/WebsiteName/"/>
</appSettings>
下一步是在您的 .Aspx 文件中创建一个超链接。
<asp:HyperLink ID="HyperLink1" runat="server" Target="_blank"></asp:HyperLink>
在后面的代码中连接 AppSettings Key + 从根目录下载文件路径。
string base_path = ConfigurationSettings.AppSettings["WebsiteURL"];
HyperLink1.NavigateUrl = base_path + "Uploads/" + ds_row["Attachment"].ToString();
如果您有任何问题,请告诉我。
所有的汗水都是由于路径问题。旧代码和新代码都有效。无需 Web 客户端代码。
这里设置的上传路径:
public static string UploadDirectory = ConfigurationManager.AppSettings["UploadDirectory"];
它指向根目录中的另一个目录,而不是应用程序所在的目录。因此,文件仅上传但从未被提取。
我暂时将其更改为AppDomain.CurrentDomain.BaseDirectory
。我们和用户谈过之后,我们会做相应的设置。
早期的开发人员使用标签放置此代码,并在 运行 时间将其变成超链接。
<asp:Label ID="lbl_Attachment" runat="server">
</asp:Label>
lbl_Attachment.Text = "<a href='../Uploads/" +
ds_row["Attachment"].ToString() + "'>" +
ds_row["Attachment"].ToString() + "</a>";
但这不起作用。所以我将代码更改为以下代码以在新的浏览器选项卡中打开任何文件 (image/pdf/word),但错误仍然存在:
hyperlinkAppDoc.NavigateUrl =
ResolveUrl("../Uploads/" +
ds_row["Attachment"].ToString());
hyperlinkAppDoc.Target = "_blank";
我该怎么做才能解决这个问题? MIME 类型在 IIS 中可用。
更新:
我正在尝试一种不同的方法。但是 Server.MapPath
指向本地驱动器而不是 wwwroot。如何指向 wwwroot 文件夹的路径?
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);
您本可以使用 asp.hyperlink
。喜欢关注
<asp:HyperLink id="hyperlink1" NavigateUrl="<set_from_code_behind>" Text="Text to redirect" runat="server"/>
并从后面的代码中设置 NavigateUrl,如下所示。
hyperlink1.NavigateUrl= "Uploads/" + ds_row["Attachment"].ToString();
根据您的情况,我建议您在网络配置文件的 <appSettings>
标记中添加一个键。
<appSettings>
<add key="WebsiteURL" value="http://localhost:1234/WebsiteName/"/>
</appSettings>
下一步是在您的 .Aspx 文件中创建一个超链接。
<asp:HyperLink ID="HyperLink1" runat="server" Target="_blank"></asp:HyperLink>
在后面的代码中连接 AppSettings Key + 从根目录下载文件路径。
string base_path = ConfigurationSettings.AppSettings["WebsiteURL"];
HyperLink1.NavigateUrl = base_path + "Uploads/" + ds_row["Attachment"].ToString();
如果您有任何问题,请告诉我。
所有的汗水都是由于路径问题。旧代码和新代码都有效。无需 Web 客户端代码。
这里设置的上传路径:
public static string UploadDirectory = ConfigurationManager.AppSettings["UploadDirectory"];
它指向根目录中的另一个目录,而不是应用程序所在的目录。因此,文件仅上传但从未被提取。
我暂时将其更改为AppDomain.CurrentDomain.BaseDirectory
。我们和用户谈过之后,我们会做相应的设置。