无法使用asp.net C中的linkbutton从服务器下载文件#

时间:2021-08-15 18:15:10

I'm try to Create linkbutton to a path in the server, but it's not working.
In addition I tried to do it with LinkButton but it still did not working.

我尝试创建服务器中的路径的链接按钮,但它不起作用。此外,我尝试使用LinkBut​​ton,但它仍然无法正常工作。

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. Please review the following URL and make sure that it is spelled correctly.

说明:HTTP 404.您要查找的资源(或其中一个依赖项)可能已被删除,名称已更改或暂时不可用。请查看以下网址,并确保拼写正确。

Requested URL: /51.doc

请求的URL:/51.doc

if i change the string HyperLinkDownload to : "HyperLinkDownload.NavigateUrl =path;" the hyperlink not responding to click, when i click after inspect element i got this error message

如果我将字符串HyperLinkDownload更改为:“HyperLinkDownload.NavigateUrl = path;”超链接没有响应点击,当我点击检查元素后,我收到此错误消息

HTTP Error 400 - Bad Request.

HTTP错误400 - 错误请求。

Version Information: ASP.NET Development Server 10.0.0.0

版本信息:ASP.NET Development Server 10.0.0.0

1 个解决方案

#1


1  

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();

#1


1  

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();