简体   繁体   English

下载和重定向

[英]Download and Redirect

I would like to download a.CSV and redirect the user to another page.我想下载 a.CSV 并将用户重定向到另一个页面。

This is my code这是我的代码

    protected void btnExport_Click(object sender, EventArgs e)
    {
        string attachment = "attachment; filename=" + Request.QueryString["exportName"] + ".csv";
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.ClearHeaders();
        HttpContext.Current.Response.AddHeader("content-disposition", attachment);
        HttpContext.Current.Response.ContentType = "application/ms-excel";

        //CODE TO WRITE CSV
        List<Company> companies = (List<Company>)Session["SelectedCompanies"];
        foreach (Company company in companies)
        {
            HttpContext.Current.Response.Write(company.Name + ";");
            HttpContext.Current.Response.Write(Environment.NewLine);
        }



        HttpContext.Current.Response.Redirect("otherpage.aspx", true);


    }

But unfortunatly, it does only the redirect and not the download of the.CSV.但不幸的是,它只做重定向,不做 .CSV 的下载。

And if I replace HttpContext.Current.Response.Redirect("otherpage.aspx", true);如果我更换HttpContext.Current.Response.Redirect("otherpage.aspx", true); by HttpContext.Current.Response.End();通过HttpContext.Current.Response.End(); , then it does only the download of the.CSV and not the redirect. , 然后它只下载 the.CSV 而不是重定向。

But I would like to have both.但我想两者兼得。

As far as I know this two actions couldn't be done in one request, either you redirect user either give file.据我所知,这两个操作不能在一个请求中完成,要么你重定向用户要么给文件。 Try to make it in two actions instead.试着用两个动作来代替。

With such cases, what I do is:对于这种情况,我所做的是:

  1. have an iframe hidden隐藏了一个 iframe

    < iframe id="DownloadIFrame" visible="false" width="1" height="1" style="display:none" > < /iframe > < iframe id="DownloadIFrame" visible="false" width="1" height="1" style="display:none" > </iframe>

  2. give this Iframe the url of my downlood (like download.ashx) while changing the content of my page.在更改我的页面内容时,将我的下载(如 download.ashx)的 Iframe 赋予 url。

On the server在服务器上

public string DownloadViaIFrame(Page page, Download download)
        {
            string script = string.Format(
            @"
            var IsDownloaded=false;

            domReady(function() {{
                if (document.getElementById('DownloadIFrame')==undefined)
                                alert('DownloadIFrame not found');
                            else
                            {{
                                if (!IsDownloaded)
                                {{
                                    {0};
                                    IsDownloaded=true;
                                }}
                            }}
            }});", GetJavasciptToDownloadViaIFrame(download));


            ScriptManager.RegisterClientScriptBlock(page, typeof(Page), "download", script, true);

            return script;
        }


 public string GetJavasciptToDownloadViaIFrame(Download download)
        {
            return string.Format("document.getElementById('DownloadIFrame').src='{0}'", GetDownloadLink(download));
        }

this way the Iframe hit the download script on the server and gives back the file to be downloaded by the client.通过这种方式,Iframe 命中了服务器上的下载脚本,并返回了客户端要下载的文件。 The client at the same time sees the content of its page changed and is happy..客户端同时看到其页面内容发生变化并且很高兴..

good luck,祝你好运,

as i know, no way making both actions.据我所知,没有办法同时采取这两项行动。 Try redirect to otherPage.aspx,and inject a javascript code in otherPage.aspx that at onload event connect to a specific "real" download page尝试重定向到 otherPage.aspx,并在 otherPage.aspx 中注入 javascript 代码,在 onload 事件中连接到特定的“真实”下载页面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM