asp.net实现下载功能 像网页上那些下载的:比如说本地下载,你点击链接的话,就会直接下载还有就是电信下载了,我感觉这些就是超链接,不知道用ASP.NET应该怎么实现呢?请大家帮帮我,谢谢我不知道这是怎么个原理,如果有代码可以借我研究一下,谢谢,呵呵 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 String FullFileName = Server.MapPath("文件路径"); FileInfo DownloadFile = new FileInfo(FullFileName); Response.Clear(); Response.ClearHeaders(); Response.Buffer = false; Response.ContentType = "application/octet-stream"; Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(DownloadFile.FullName, System.Text.Encoding.UTF8)); Response.AppendHeader("Content-Length", DownloadFile.Length.ToString()); Response.WriteFile(DownloadFile.FullName); Response.Flush(); Response.End(); 连接的URL就写你的文件存放路径 相对路径。 注意打包文件,比如TXT BMP这种浏览器直接显示而不是下载至于你说的点击直接打开迅雷 我估计是一种 协议, 也是写在URL里的。 网上搜下吧 #region 下载服务器上的文件 /// <summary> /// 下载服务器上的文件 /// </summary> /// <param name="PageResponse">程序中可以设置参数:HttpResponse ht=Page.Response;</param> /// <param name="serverPath">服务器上的文件路径</param> public void DownloadFile(HttpResponse response, string serverPath) { FileStream fs = null; try { fs = File.OpenRead(serverPath); byte[] buffer = new byte[1024]; long count = 1024; response.Buffer = true; response.AddHeader("Connection", "Keep-Alive"); response.ContentType = "application/octet-stream"; response.AddHeader("Content-Disposition", "attachment;filename=" + Path.GetFileName(serverPath));//下载时要保存的默认文件名 response.AddHeader("Content-Length", fs.Length.ToString()); while (count == 1024) { count = fs.Read(buffer, 0, 1024); response.BinaryWrite(buffer); } } catch { } finally { fs.Close(); } } #endregion指定路径就好了 asp.net下载文件的常用方法http://blog.sina.com.cn/s/blog_5dfb7f4d0100c3nh.html .NET(C#)技术交流 35032539 .NET(C#)技术交流 35032539 .NET(C#)技术交流 35032539 private void StartReceive(){ IPEndPoint ipep=new IPEndPoint(IPAddress.Parse(this.textBox1.Text),int.Parse(this.textBox5.Text)); Socket client=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); client.Connect(ipep); string SendFileName=System.Text.Encoding.Unicode.GetString(CommonModule.EzoneModule.ReceiveVarData(client)); this.textBox2.Text=SendFileName; this.textBox3.Text=System.Text.Encoding.Unicode.GetString(CommonModule.EzoneModule.ReceiveVarData(client)); this.textBox8.Text=System.Text.Encoding.Unicode.GetString(CommonModule.EzoneModule.ReceiveVarData(client)); this.progressBar1.Maximum=int.Parse(this.textBox8.Text); this.textBox9.Text=System.Text.Encoding.Unicode.GetString(CommonModule.EzoneModule.ReceiveVarData(client)); FileStream MyFileStream=new FileStream(SendFileName,FileMode.Create,FileAccess.Write); int SendedCount=0; while(true) { byte[] data=CommonModule.EzoneModule.ReceiveVarData(client); if(data.Length==0) { break; } else { SendedCount++; MyFileStream.Write(data,0,data.Length); this.textBox10.Text=SendedCount.ToString(); this.progressBar1.PerformStep(); }} this.progressBar1.Value=this.progressBar1.Maximum; //关闭文件流 MyFileStream.Close(); //关闭套接字 client.Close(); this.button1.Enabled=true; MessageBox.Show("文件接收完毕!"); } CSDN 查一下webclient 这个是最简单的 以前就用的超链接直接在href里写路径就可以了,今天弄了好久也不行....郁闷..... asp.net如何判断某字符串是什么UTF-8编码还是GB2312编码 太郁闷了,明明是对的,为什么不可以???? 一个小问题,获得当前web网站登录用户的用户名 邮件发不出去,急,大家帮忙呀。 如何使用验证控件验证checkboxlist中至少选择一个? 怎么让TreeView 控件的结点,变成单选框的形式! 数据库连接不上??? asp.net的treeview控件 判断某个文件是否存在~ 有没有必要学下MVC? 未能找到任何适合于指定的区域性或非特定区域性的资源。请确保在编译时已将“xxxx.resources”正确嵌入或链接到程序集“xxx” 深圳网软通公司怎么样?
FileInfo DownloadFile = new FileInfo(FullFileName);
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(DownloadFile.FullName, System.Text.Encoding.UTF8));
Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
Response.WriteFile(DownloadFile.FullName);
Response.Flush();
Response.End();
/// <summary>
/// 下载服务器上的文件
/// </summary>
/// <param name="PageResponse">程序中可以设置参数:HttpResponse ht=Page.Response;</param>
/// <param name="serverPath">服务器上的文件路径</param>
public void DownloadFile(HttpResponse response, string serverPath)
{
FileStream fs = null;
try
{
fs = File.OpenRead(serverPath);
byte[] buffer = new byte[1024];
long count = 1024;
response.Buffer = true;
response.AddHeader("Connection", "Keep-Alive");
response.ContentType = "application/octet-stream";
response.AddHeader("Content-Disposition", "attachment;filename=" + Path.GetFileName(serverPath));//下载时要保存的默认文件名
response.AddHeader("Content-Length", fs.Length.ToString());
while (count == 1024)
{
count = fs.Read(buffer, 0, 1024);
response.BinaryWrite(buffer);
}
}
catch
{
}
finally
{
fs.Close();
}
}
#endregion指定路径就好了
http://blog.sina.com.cn/s/blog_5dfb7f4d0100c3nh.html
.NET(C#)技术交流 35032539
.NET(C#)技术交流 35032539
{
IPEndPoint ipep=new IPEndPoint(IPAddress.Parse(this.textBox1.Text),int.Parse(this.textBox5.Text));
Socket client=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
client.Connect(ipep);
string SendFileName=System.Text.Encoding.Unicode.GetString(CommonModule.EzoneModule.ReceiveVarData(client));
this.textBox2.Text=SendFileName;
this.textBox3.Text=System.Text.Encoding.Unicode.GetString(CommonModule.EzoneModule.ReceiveVarData(client));
this.textBox8.Text=System.Text.Encoding.Unicode.GetString(CommonModule.EzoneModule.ReceiveVarData(client));
this.progressBar1.Maximum=int.Parse(this.textBox8.Text);
this.textBox9.Text=System.Text.Encoding.Unicode.GetString(CommonModule.EzoneModule.ReceiveVarData(client));
FileStream MyFileStream=new FileStream(SendFileName,FileMode.Create,FileAccess.Write);
int SendedCount=0;
while(true)
{
byte[] data=CommonModule.EzoneModule.ReceiveVarData(client);
if(data.Length==0)
{
break;
}
else
{
SendedCount++;
MyFileStream.Write(data,0,data.Length);
this.textBox10.Text=SendedCount.ToString();
this.progressBar1.PerformStep();
}
}
this.progressBar1.Value=this.progressBar1.Maximum;
//关闭文件流
MyFileStream.Close();
//关闭套接字
client.Close();
this.button1.Enabled=true;
MessageBox.Show("文件接收完毕!");
}