不能在asp.net C#中使用的LinkBut​​ton从服务器下载文件 [英] can't download file from server using linkbutton in asp.net C#

查看:208
本文介绍了不能在asp.net C#中使用的LinkBut​​ton从服务器下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试创建的LinkBut​​ton到服务器的路径,但它不工作。结果
此外,我试着用的LinkBut​​ton做到这一点,但它仍然没有工作。

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 />

错误消息:

资源无法找到。

说明:HTTP 404。您正在寻找(或它的一个依赖)可能已被删除的资源,更名或暂时不可用。请检查以下URL并确保其拼写正确。

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.

请求的URL:/51.doc

Requested URL: /51.doc

如果我改变字符串HyperLinkDownload到:HyperLinkDownload.NavigateUrl =路径;
超链接不响应点击,当我点击检查元素我得到这个错误消息后

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错误400 - 错误的请求

HTTP Error 400 - Bad Request.

版本信息:ASP.NET开发服务器10.0.0.0

Version Information: ASP.NET Development Server 10.0.0.0

推荐答案

使用使用Server.Mappath(你的目标文件),而不是手工编写的路径@U:/ HR /恢复.... bla..bla ........

Use Server.MapPath("your destination file") instead of manually write the path @"U:/HR/Resume....bla..bla..." ..

和也试试这个code,如果下载仍然没有工作。

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

这篇关于不能在asp.net C#中使用的LinkBut​​ton从服务器下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆