首页 新闻 会员 周边

页面重定向到一个有文件下载功能的页面时,跳转不了

0
悬赏园豆:5 [待解决问题]

现在有test1.aspx和test2.aspx两个页面,我要点击test1.aspx的按钮跳转到test2.aspx页面即Response.Redirect("test2.aspx"),而在test2.aspx中的Page_Load事件中则有
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class test2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string path = HttpContext.Current.Server.MapPath("~/js/DD_belatedPNG_0.0.8a-min.js");//路径
            if (File.Exists(path))  //如果文件存在
            {
                FileInfo fileInfo = new FileInfo(path);
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.ClearContent();
                HttpContext.Current.Response.ClearHeaders();
                HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("下载名称.js"));//HttpUtility.UrlEncode防止文件下载时"文件名"出现中文乱码
                HttpContext.Current.Response.AddHeader("Content-Length", fileInfo.Length.ToString());
                HttpContext.Current.Response.AddHeader("Content-Transfer-Encoding", "binary");
                HttpContext.Current.Response.ContentType = "application/octet-stream";
                HttpContext.Current.Response.ContentEncoding = Encoding.GetEncoding("gb2312");
                HttpContext.Current.Response.WriteFile(fileInfo.FullName);
                HttpContext.Current.Response.Flush();
                HttpContext.Current.Response.End();
            }
        }
    }
}
当点击test1.aspx的按钮想要跳转到test2.aspx页面时,就出现了下载对话框,而且下载完后都没有跳转到test2.aspx页面,还是停留在test1.aspx页面
我想要实现的效果是先跳到test2.aspx页面后才开始下载文件!
先谢谢各位了!

Angel0323的主页 Angel0323 | 初学一级 | 园豆:197
提问于:2013-01-30 11:49
< >
分享
所有回答(3)
0

这个应该是游览器本身的行为吧,如果是,估计很难改变

chenping2008 | 园豆:9836 (大侠五级) | 2013-01-30 14:36
0

这个完全可以用DataHandler实现下载没有必要跳到另外一个页面下载

小-_-戴 | 园豆:9 (初学一级) | 2013-01-30 15:32
0

因为test2.aspx页面Content-Disposition是attachment。所以浏览器将test2.aspx视作文件下载,直接出现下载对话框,这其实就是你所谓“转到test2.aspx”的浏览器的表现形式。

 

如果需要在test2上停留再开始下载,那把test2做成一个页面,在页面载入后用客户端javascript脚本开始下载。

clyde.lin | 园豆:204 (菜鸟二级) | 2013-01-30 17:57
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册