0

.CSV をダウンロードして、ユーザーを別のページにリダイレクトしたいと考えています。

これは私のコードです

    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);


    }

残念ながら、リダイレクトのみを行い、.CSV のダウンロードは行いません。

で置き換えると、.CSV のダウンロードのみが行わHttpContext.Current.Response.Redirect("otherpage.aspx", true);HttpContext.Current.Response.End();、リダイレクトは行われません。

しかし、私は両方を持っていたいです。

4

3 に答える 3

2

私が知る限り、この 2 つのアクションは 1 つの要求で実行できませんでした。ユーザーをリダイレクトするか、ファイルを指定します。代わりに、2 つのアクションで作成してみてください。

于 2012-04-06T07:03:25.020 に答える
1

そのような場合、私がすることは次のとおりです。

  1. iframe を非表示にする

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

  2. ページのコンテンツを変更しながら、この Iframe にダウンロードの URL (download.ashx など) を指定します。

サーバー上

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));
        }

このようにして、Iframe はサーバー上のダウンロード スクリプトをヒットし、クライアントがダウンロードするファイルを返します。クライアントは同時に、ページのコンテンツが変更され、満足しているのを確認します..

幸運を、

于 2012-04-06T08:14:27.403 に答える
0

私が知っているように、両方のアクションを行う方法はありません。otherPage.aspx へのリダイレクトを試み、onload イベントで特定の「実際の」ダウンロード ページに接続する javascript コードを otherPage.aspx に挿入します。

于 2012-04-06T07:18:02.380 に答える