首页 新闻 会员 周边

这段下载代码有什么问题吗

0
悬赏园豆:20 [已解决问题] 解决于 2013-08-21 15:08
try
                {
                    string filepath = context.Server.MapPath(context.Request.QueryString["filedir"].ToString() + "/" + context.Request.QueryString[0].Substring(0, 4).ToString() + "/" + context.Request.QueryString[0].ToString());
                    string filename = context.Request.QueryString[1].ToString();                  
                    FileInfo info = new FileInfo(filepath); 
                    long fileSize = info.Length; 
                    context.Response.Clear(); 
                    context.Response.ContentType = "application/x-zip-compressed";
                    context.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8)); 
                    //不指明Content-Length用Flush的话不会显示下载进度 
                    context.Response.AddHeader("Content-Length", fileSize.ToString()); 
                    context.Response.TransmitFile(filepath, 0, fileSize); 
                    context.Response.Flush(); 
                    context.Response.Close(); 
                }
                catch
                { }

为什么会在99%时卡住,要等个几分钟才结束,也不是每次都这样,求指导,或者谁给个自己用的顺手的下载代码,谢谢。

happydaily的主页 happydaily | 菜鸟二级 | 园豆:301
提问于:2013-08-18 08:39
< >
分享
最佳答案
2

你把 

context.Response.Flush(); 
context.Response.Close(); 
换成
context.Response.End(); 
 
收获园豆:20
Yu | 专家六级 |园豆:12980 | 2013-08-18 10:40

如果换成

context.Response.End(); 我敢保证,这段代码每次都会执行catch
小AI | 园豆:354 (菜鸟二级) | 2013-08-18 13:29

建议这样试一下

dudu | 园豆:31003 (高人七级) | 2013-08-18 17:49

@小AI: 要保留这个 

context.Response.Flush();

Yu | 园豆:12980 (专家六级) | 2013-08-18 22:07

@Yu: 试试这个

http://support.microsoft.com/kb/812406/zh-cn

Yu | 园豆:12980 (专家六级) | 2013-08-18 22:13
其他回答(2)
0
这个是把文件当做zip格式传递给客户端,和fileSize 大小有关吧,越大的文件时间越长
2012 | 园豆:21230 (高人七级) | 2013-08-18 09:05
0

在Catch中调试或者加日志记录,我怀疑有可能爆异常了 。TransmitFile这个方法拿来干什么的?

小AI | 园豆:354 (菜鸟二级) | 2013-08-18 10:19
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册