All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
用PHP写mail时的注意事项_[PHP教程]
自己在做项目过程中学到的PHP知识收集_php资料_编程技术
C#程序设计入门经典之C#的基本语法_[Asp.Net教程]
简单介绍.Net3.0,中跨线程访问控件_.net资料_编程技术
javascript自定义模式对话框_JavaScript技术_编程技术
去掉ul全局配置及smarty的问题_php资料_编程技术
用SQL,Server,Prompt提高开发能力_[SQL,Server教程]
网络编程经验技巧:ASP.NET2.0泛型和匿名方法_.net资料_编程技术
理解掌握ASP.NET与客户端缓存之HTTP协议_.net资料_编程技术
C#教程:读取注册表信息
Reading number is top 10 pictures
Magnificent cloud2
In 2013 hercules Arnold classic2
夕阳下的北京街道
男人们都在想什么
Earthquake hedge common sense
俞敏洪在清华终于说了实话
The Soviet union swimsuit exposure in the 70 year1
Female model behind the bitterness, often being overcharged3
如果我是导演...
小学生作文又现神作,你不得不佩服
Download software ranking
打鸟视频
c#程序设计案例教程
Tram sex maniac 2 (H) rar bag14
Ashlynn Video2
天龙八部十二宫服务端
豪门浪荡史
实战黑客不求人
C#COM编程指南
C++编程教程第三版
Professional killers2 data package
delv published in(发表于) 2014/1/10 6:28:03 Edit(编辑)
ASP.NET技巧:下载文件关闭窗体之解决方法_[Asp.Net教程]

ASP.NET技巧:下载文件关闭窗体之解决方法_[Asp.Net教程]

ASP.NET技巧:下载文件关闭窗体之解决方法_[Asp.Net教程]

对于下载时新打开一窗口下载,下载完后需要关闭的
一种:对于较小文件可以有效:
System.IO.FileInfo file = new System.IO.FileInfo("F:\\mp3\\mp3\\别哭我最爱的人.mp3");
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Response.Charset="GB2312";
Response.ContentEncoding=System.Text.Encoding.UTF8;
Response.ContentType = "application/octet-stream";

Response.AddHeader("Content-Disposition", "attachment; filename="+Server.UrlEncode("别哭我最爱的人.mp3"));
Response.AddHeader("Content-Length", file.Length.ToString());



Response.WriteFile(file.FullName);
Response.Flush();
Response.End();
该代码可以实现输出文件并将新打开的窗体关闭。


另一种对于大文件,我们用上述代码不是较好,因为上述代码是将文件全缓存于服务器后才输出的,如果你的服务器流量大,且文件较大,不建议用以上方法。


以下我测试过,较好能解决:
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 = (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;
}
}
_Response.Flush();
}
catch
{
return false;
}
finally
{
br.Close();
myFile.Close();
}
}
catch
{
return false;
}
return true;
}
这一种代码也是借签网上的
它也可以实现输出文件并关闭新打开幕窗体


另外在测试当中发现,服务器端与客户的操作系统不同
也会有一些意想不到的事情发生
一般来说,用以上代码,如不能正常关闭,你可以查阅浏览器的设置,在高级中。
再者,你可以修改不同的输出内容类型Response.ContentType = "application/octet-stream";因为不同的contenttype,在客户的端的解释可能是不一样的。
有想法请回复,欢迎讨论


来源:网络







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.