求文件下载的代码!

lyb_abiandbel 2005-04-19 12:20:34
我想用.net做下载,请问哪位高人有这方面的代码,让我参考一下啊!
...全文
1142 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
FJGoodGood 2005-05-17
  • 打赏
  • 举报
回复
2楼在瞎说。
saintqiqi 2005-05-12
  • 打赏
  • 举报
回复
look
yuranccc2003 2005-05-12
  • 打赏
  • 举报
回复
up
lyb_abiandbel 2005-05-11
  • 打赏
  • 举报
回复
直接给连接和这样写代码有什么区别吗?
bettybetty 2005-05-10
  • 打赏
  • 举报
回复
我刚刚做了.给你一个.

public static bool ResponseFile(HttpRequest _Request,HttpResponse _Response,string _fileName,string _fullPath, long _speed)
{
try
{
FileStream myFile = new FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
BinaryReader br = new BinaryReader(myFile);
try
{
_Response.AddHeader("Accept-Ranges", "bytes");
_Response.Buffer = false;
long fileLength = myFile.Length;
long startBytes = 0;

int pack = 10240; //10K bytes
//int sleep = 200; //每秒5次 即5*10K bytes每秒
int sleep = (int)Math.Floor(1000 * pack / _speed) + 1;
if (_Request.Headers["Range"] != null)
{
_Response.StatusCode = 206;
string[] range = _Request.Headers["Range"].Split(new char[] {'=', '-'});
startBytes = Convert.ToInt64(range[1]);
}
_Response.AddHeader("Content-Length", (fileLength - startBytes).ToString());
if (startBytes != 0)
{
_Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength-1, fileLength));
}
_Response.AddHeader("Connection", "Keep-Alive");
_Response.ContentType = "application/octet-stream";
_Response.AddHeader("Content-Disposition","attachment;filename=" + HttpUtility.UrlEncode(_fileName,System.Text.Encoding.UTF8) );

br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
int maxCount = (int) Math.Floor((fileLength - startBytes) / pack) + 1;

for (int i = 0; i < maxCount; i++)
{
if (_Response.IsClientConnected)
{
_Response.BinaryWrite(br.ReadBytes(pack));
Thread.Sleep(sleep);
}
else
{
i=maxCount;
}
}
}
catch
{
return false;
}
finally
{
br.Close();
myFile.Close();
}
}
catch
{
return false;
}
return true;
}



上面是函数
下面是引用   Session["path"]是 文件名

string FileName=Request.PhysicalApplicationPath+Session["path"];
Page.Response.Clear();
string str=Session["path"].ToString();
bool success = ResponseFile(Page.Request, Page.Response, str,FileName, 1024000);

if(!success)
Response.Write("下载文件出错!");

Page.Response.End();

lyb_abiandbel 2005-05-10
  • 打赏
  • 举报
回复
up
yulitian 2005-04-30
  • 打赏
  • 举报
回复
using System;
using System.Web;
using System.IO;
using System.Threading;
namespace Soft1000.Libs.DownLoad
{
/// <summary>
/// DownLoad 的摘要说明。
/// </summary>
public class DownLoad
{
public DownLoad()
{
//
// TODO: 在此处添加构造函数逻辑
//
}


/// <summary>
/// 输出硬盘文件,提供下载
/// </summary>
/// <param name="_Request">Page.Request对象</param>
/// <param name="_Response">Page.Response对象</param>
/// <param name="_fileName">下载文件名</param>
/// <param name="_fullPath">带文件名下载路径</param>
/// <param name="_speed"><每秒允许下载的字节数/param>
/// <returns>返回是否成功</returns>
public static bool ResponseFile(HttpRequest _Request,HttpResponse _Response,string _fileName,string _fullPath, long _speed)
{
try
{
//建立文件对象
FileStream myFile = new FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
//建立流读对象
BinaryReader br = new BinaryReader(myFile);
try
{
_Response.AddHeader("Accept-Ranges", "bytes");
_Response.Buffer = false;
long fileLength = myFile.Length;
long startBytes = 0;

int pack = 10240; //10K bytes
//int sleep = 200; //每秒5次 即5*10K bytes每秒
int sleep = (int)Math.Floor(1000 * pack / _speed) + 1;
if (_Request.Headers["Range"] != null)
{
_Response.StatusCode = 206;
string[] range = _Request.Headers["Range"].Split(new char[] {'=', '-'});
startBytes = Convert.ToInt64(range[1]);
}
_Response.AddHeader("Content-Length", (fileLength - startBytes).ToString());
if (startBytes != 0)
{
_Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength-1, fileLength));
}
_Response.AddHeader("Connection", "Keep-Alive");
_Response.ContentType = "application/octet-stream";
_Response.AddHeader("Content-Disposition","attachment;filename=" + HttpUtility.UrlEncode(_fileName,System.Text.Encoding.UTF8) );

br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
int maxCount = (int) Math.Floor((fileLength - startBytes) / pack) + 1;
for (int i = 0; i < maxCount; i++)
{
if (_Response.IsClientConnected)
{
_Response.BinaryWrite(br.ReadBytes(pack));
Thread.Sleep(sleep);
}
else
{
i=maxCount;

}
}
}
catch
{
return false;
}
finally
{
br.Close();
myFile.Close();
}
}
catch
{
return false;
}
return true;
}

}
}
lyb_abiandbel 2005-04-30
  • 打赏
  • 举报
回复
upupup
lyb_abiandbel 2005-04-23
  • 打赏
  • 举报
回复
up
lyb_abiandbel 2005-04-22
  • 打赏
  • 举报
回复
对啊,怎么可能不行呢?
wula0010 2005-04-21
  • 打赏
  • 举报
回复
我的这个就可以让客户下载选择的文件:
s_fileName为文件名(带路径),是通过其他地方传过来的。
Response.ContentType = "application/ms-download";
System.IO.FileInfo file = new System.IO.FileInfo(s_fileName);
Response.Clear();
Response.AddHeader("Content-Type", "application/octet-stream");
Response.Charset = "utf-8";
Response.AddHeader("Content-Disposition", "attachment;filename="+System.Web.HttpUtility.UrlEncode(file.Name,System.Text.Encoding.UTF8));
Response.AddHeader("Content-Length", file.Length.ToString());
Response.WriteFile(file.FullName);
Response.Flush();
Response.Clear();
Response.End();
lyb_abiandbel 2005-04-21
  • 打赏
  • 举报
回复
ren ne???
lyb_abiandbel 2005-04-20
  • 打赏
  • 举报
回复
up
lyb_abiandbel 2005-04-20
  • 打赏
  • 举报
回复
看来得自己搞定了!
lyb_abiandbel 2005-04-19
  • 打赏
  • 举报
回复
给个网址或贴点代码啊!急!
simon8181 2005-04-19
  • 打赏
  • 举报
回复
Response.Clear()
Response.ContentType = "Application/octet-stream"
Response.ContentEncoding = System.Text.Encoding.UTF8
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(Right(file.Name, file.Name.Length - 8)))
Response.AddHeader("Content-Length", file.Length.ToString())
Response.WriteFile(file.FullName)
Response.End()
iloveyour 2005-04-19
  • 打赏
  • 举报
回复
Public Function ResponseFile(ByVal _Request As HttpRequest, ByVal _Response As HttpResponse, ByVal _fileName As String, ByVal _fullPath As String, ByVal _speed As Long) As Boolean

Try
Dim myFile As FileStream = New FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
Dim br As BinaryReader = New BinaryReader(myFile)
Try
_Response.AddHeader("Accept-Ranges", "bytes")
_Response.Buffer = False
Dim fileLength As Long = myFile.Length
Dim startBytes As Long = 0
Dim pack As Int16 = 10240
Dim sleep As Int16 = Convert.ToInt16(Math.Floor(1000 * pack / _speed) + 1)

If Not _Request.Headers("Range") Is Nothing Then
_Response.StatusCode = 206
Dim splitChar(1) As Char
splitChar(0) = "="
splitChar(1) = "-"

Dim range() As String = _Request.Headers("Range").Split("a")
startBytes = Convert.ToInt64(range(1))
End If

_Response.AddHeader("Content-Length", (fileLength - startBytes).ToString())
If (startBytes <> 0) Then
_Response.AddHeader("Content-Range", String.Format(" bytes {0}-{1}/{2}", startBytes, fileLength - 1, fileLength))
End If

_Response.AddHeader("Connection", "Keep-Alive")
_Response.ContentType = "application/octet-stream"
_Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(_fileName, System.Text.Encoding.UTF8))

br.BaseStream.Seek(startBytes, SeekOrigin.Begin)
Dim maxCount As Long = Convert.ToInt64(Math.Floor((fileLength - startBytes) / pack) + 1)
Dim i As Long

For i = 0 To maxCount
If (_Response.IsClientConnected) Then
_Response.BinaryWrite(br.ReadBytes(pack))
Thread.Sleep(sleep)
Else
i = maxCount
End If
Next

Catch e As Exception

Return False
Finally
br.Close()
myFile.Close()
End Try

Catch
Return False
End Try

End Function
syeerzy 2005-04-19
  • 打赏
  • 举报
回复
下载,顾名思义是客户端要下,所以载。你硬塞給人家那不叫下载,那其实是“从服务器‘上传’到客户端”。

一楼的做法是向客户端“写一个”你打算他下载的“文件”,在C/S结构中是没有问题的,因为C端来写。但是在Asp.net却是不行的,一定会弹出权限的错误,因为B/S结构中没有个C端給你,S端是不可能获得客户机硬盘的写权限的,要不还不是世界大乱了?

上传和下载的根本区别在于一个是“本地主动送”,一个是“本地主动收”。他们都需要有一方来“主动”,上传需要对远程有写权限,下载需要对远程有读权限。现在的情况是服务器根本对客户端没有读写权限,所以实际上在B/S里写这种代码没有用。


换个思路,我不要服务器主动,我让客户端主动可以吗?
1、IE的下载功能。包括Http协议中规定的下载(对IE本身无法打开的连接文件类型会提示用户是否下载)这是最方便的,你只要給个超级连接就行了,一点代码都不用。局限性在于只对“当前IE无法打开的文件类型”有效。这就是为什么同个东西有些人点了打开浏览,有些人是提示下载,就是浏览器不同(包括组件、版本等)。
2、javascript主动向服务器请求文件,并由js向硬盘上写。(本人没尝试过,所以到底可行与否不知道)
3、由浏览器以外的客户端程序来执行写操作。C/S版因为有客户端,简单的和什么一样。。。B/S版里有什么是“浏览器以外的客户端程序”而又是你可以访问的??没错,木马!放个木马绝对没问题(废话。。)至于怎么放那就是另一方面的事情了。。。
4、让客户端以为这是IE的插件(这也许是,也许不是,不是你就要伪装来骗它),这是3721等喜欢使用的方法。局限性在于和浏览器的设置有关,如果设置关掉相应的自动选项,你急也没办法!

62,047

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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