简体   繁体   中英

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.
In addition I tried to do it with LinkButton but it still did not working.

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: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.

Requested 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

HTTP Error 400 - Bad Request.

Version Information: ASP.NET Development Server 10.0.0.0

Use Server.MapPath("your destination file") instead of manually write the path @"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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM