在webform中如何实现下载文件的功能呢?谢谢

newdreamer 2005-12-22 11:23:55
如题,知道下载网址后,点击按钮下载。
...全文
448 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
newdreamer 2005-12-23
  • 打赏
  • 举报
回复
小山的那段代码,我试了没反映。
zeusvenus 2005-12-23
  • 打赏
  • 举报
回复
主要用到webrequest/webclient等类。
用Visual C#实现文件下载教程:
http://www.lzhm.net/read.aspx?ID=73

如果只想下载该页面,用孟子E章网站上提供的代码即可。
hainang1234 2005-12-23
  • 打赏
  • 举报
回复
楼上这样就行了,简单!

除非文件是动态生成的。
bobibobi 2005-12-23
  • 打赏
  • 举报
回复
response.redirct("http://files4.rarlab.com/rar/wrar351.exe");
newdreamer 2005-12-22
  • 打赏
  • 举报
回复
这段代码是下载到那里呀?
gqchen1981 2005-12-22
  • 打赏
  • 举报
回复
在你的点击事件里:
FileInfo file = new FileInfo(你服务器上的文件路径及名);
Response.Clear();
Response.ClearHeaders();
Response.Buffer=false;
Response.ContentType="application/octet-stream";
Response.AppendHeader("Content-Disposition","attachment;filename=" + HttpUtility.UrlEncode(file.FullName,System.Text.Encoding.UTF8));
Response.AppendHeader("Content-Length",file.Length.ToString());
Response.WriteFile(file.FullName);
Response.Flush();
singlepine 2005-12-22
  • 打赏
  • 举报
回复
public static void DownloadFile(string physicalFilePath)
{
FileStream stream=null;
try
{
stream = new FileStream(physicalFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
int bufSize = (int)stream.Length;
byte[] buf = new byte[bufSize];

int bytesRead = stream.Read(buf, 0, bufSize);
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename="+System.IO.Path.GetFileName(physicalFilePath));
HttpContext.Current.Response.OutputStream.Write(buf, 0, bytesRead);
HttpContext.Current.Response.End();
}
finally
{
stream.Close();
}
}
豌豆干 2005-12-22
  • 打赏
  • 举报
回复
FileInfo file = new FileInfo(你服务器上的文件路径及名);

这个地址是指C:\Inetpub\wwwroot形式的地址,不是指url
newdreamer 2005-12-22
  • 打赏
  • 举报
回复
我执行第一行时: FileInfo file = new FileInfo("http://files4.rarlab.com/rar/wrar351.exe");
提示:不支持 URI 格式
如何解决呢?

110,477

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧