| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 618 人关注过本帖
标题:[分享]实现直接从网页上下载文件,而不须引用文件URL来下载
只看楼主 加入收藏
cyyu_ryh
Rank: 8Rank: 8
等 级:贵宾
威 望:45
帖 子:1899
专家分:176
注 册:2006-10-21
结帖率:100%
收藏
 问题点数:0 回复次数:1 
[分享]实现直接从网页上下载文件,而不须引用文件URL来下载
老斑不要删了哦


'函数名:ResponseFile
'功能 :客户端从服务器端下载一个文件
'返回值:返回True表示服务器响应成功,返回False表示失败
'参数 :
' PageResponse 响应客户端的Response对象,用Page.Response引用
' DownloadFileName 客户端下载文件的文件名
' LocalFilePath 服务器端待下载文件的路径
' DownloadBuffer 服务器端读取文件的缓冲区大小,单位为KB
Public Function ResponseFile(ByRef PageResponse As HttpResponse, ByVal DownloadFileName As String, ByVal LocalFilePath As String, ByVal DownloadBuffer As Long) As Boolean
Dim Reader As System.IO.FileStream
Dim Buffer() As Byte
Dim FileLength As Long
Dim FileBuffer As Long = 1024 * DownloadBuffer
Dim ReadCount As Long
ReadCount = FileBuffer
ReDim Buffer(ReadCount - 1)
Try
Reader = System.IO.File.OpenRead(LocalFilePath)
FileLength = Reader.Length
Try
PageResponse.Buffer = False
PageResponse.AddHeader("Connection", "Keep-Alive")
PageResponse.ContentType = "application/octet-stream"
PageResponse.AddHeader("Content-Disposition", "attachment;filename=" + DownloadFileName)
PageResponse.AddHeader("Content-Length", FileLength.ToString)
While ReadCount = FileBuffer
ReadCount = Reader.Read(Buffer, 0, FileBuffer)
ReDim Preserve Buffer(ReadCount - 1)
PageResponse.BinaryWrite(Buffer)
End While
Response.End()
Catch ex As Exception
Return False
Finally
Reader.Close()
End Try
Catch ex As Exception
Return False
End Try
Return True
End Function
搜索更多相关主题的帖子: 网页 URL 文件 分享 
2007-03-22 14:05
卡洛
Rank: 2
等 级:论坛游民
威 望:2
帖 子:185
专家分:40
注 册:2007-3-23
收藏
得分:0 

嘿嘿。vb的哦。我来提供个c#的
string strFile = Server.MapPath("文件路径");//路径根据实际情况而定
if (!System.IO.File.Exists(strFile))
{
Response.Write("<script language='javascript'>alert('对不起,文件不存在!');</script>");
Response.Redirect("downlist.aspx");
return;
}
Response.Clear();
Response.ClearHeaders();
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ContentType = "application/octet-stream";
FileInfo fi = new FileInfo(strFile);
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fi.Name));
Response.AddHeader("Content-Length", fi.Length.ToString());
byte[] tmpbyte = new byte[1024 * 8];
FileStream fs = fi.OpenRead();
int count;
while ((count = fs.Read(tmpbyte, 0, tmpbyte.Length)) > 0)
{
Response.BinaryWrite(tmpbyte);
Response.Flush();
}
fs.Close();
Response.End();


2007-03-23 14:58
快速回复:[分享]实现直接从网页上下载文件,而不须引用文件URL来下载
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020972 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved