[英]can't download file from server using linkbutton in asp.net C#
I'm try to Create linkbutton to a path in the server, but it's not working. 我正在尝试创建指向服务器中路径的linkbutton,但是它不起作用。
In addition I tried to do it with LinkButton but it still did not working. 此外,我尝试使用LinkButton进行操作,但仍然无法正常工作。
c#: C#:
string path = "U:\\HR\\resume\\System\\" + Department + "\\" + ID + extFile;
if (File.Exists(path))
{
HyperLinkDownload.ID = ID.ToString();
lbResumeExist.Text = "File Exists";
HyperLinkDownload.Text = "download";
HyperLinkDownload.NavigateUrl = ID + ext.ToString();
LinkButton1.Text = "download";
LinkButton1.PostBackUrl= path;
}
else
{
HyperLinkDownload.Visible = false;
lbResumeExist.Visible = false;
LinkButton1.Visible = false;
}
asp: ASP:
<asp:HyperLink ID="HyperLinkDownload" runat="server"></asp:HyperLink>
<br /><br />
<asp:LinkButton ID="LinkButton1" runat="server"></asp:LinkButton>
<br /><br />
error message: 错误信息:
The resource cannot be found. 无法找到该资源。
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. 说明:HTTP404。您正在寻找的资源(或其依赖项之一)可能已被删除,名称更改或暂时不可用。 Please review the following URL and make sure that it is spelled correctly.
请查看以下网址,并确保其拼写正确。
Requested URL: /51.doc 要求的网址:/51.doc
if i change the string HyperLinkDownload to : "HyperLinkDownload.NavigateUrl =path;" 如果我将字符串HyperLinkDownload更改为:“ HyperLinkDownload.NavigateUrl = path;” the hyperlink not responding to click, when i click after inspect element i got this error message
超链接不响应单击,当我在检查元素后单击时,收到此错误消息
HTTP Error 400 - Bad Request. HTTP错误400-错误的请求。
Version Information: ASP.NET Development Server 10.0.0.0 版本信息:ASP.NET开发服务器10.0.0.0
Use Server.MapPath("your destination file") instead of manually write the path @"U:/HR/Resume....bla..bla..." .. 使用Server.MapPath(“您的目标文件”)而不是手动编写路径@“ U:/ HR / Resume .... bla..bla ...” ..
and also try this code if the download still not working.. 如果下载仍无法进行,请尝试此代码。
// send the PDF document as a response to the browser for download
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.ContentType = "application/pdf";
if (!displayOnBrowser)
{ response.AddHeader("Content-Disposition", String.Format("attachment; filename=GettingStarted.pdf; size={0}", pdfBytes.Length.ToString()));
}
else
{ response.AddHeader("Content-Disposition", String.Format("inline; filename=GettingStarted.pdf; size={0}", pdfBytes.Length.ToString()));
}
response.BinaryWrite(pdfBytes);
// Note: it is important to end the response, otherwise the ASP.NET
// web page will render its content to PDF document stream
response.End();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.