1

ページ(およびデータベース)から情報を収集し、指定された情報からcsvファイルを作成し、作成されたファイルをダウンロードしてページを更新することは不可能ですか?

現在、ユーザーには行のあるテーブルが表示され、チェックボックスをオンにして行を選択し、[エクスポート] ボタンを押すと、CSV ファイルが作成され、ユーザーのコンピューターにダウンロードされます。問題はページの更新にあります。

現在、ボタンをクリックすると、次の方法で CSV が作成およびダウンロードされます。

        //here is create csv function and then the result is outputed
        Response.Clear()
        Response.Buffer = True
        Response.AddHeader("content-disposition", "attachment;filename=DataTable.csv")        
        Response.Charset = ""
        Response.ContentType = "application/text"

        Response.Output.Write(CsvFile.ToString(), 0)
        Response.Flush()
        Response.End()

この問題により、csv ファイルがダウンロードされた後のページの更新で問題が発生します。

1) このリンク: C# で Response.End() が呼び出された後に別のページにリダイレクトする

`Response.AddHeader("Refresh", "3; url=index.html"); を提案します。ページを更新する必要があります。これは機能しません。リダイレクトは行われません。

2) 提案された別の解決策は、追加の URL を作成し、ユーザーが特定の URL からファイルをダウンロードできるようにすることです。ファイルはページ上のデータから作成されるため、これは私の状況には合いません。

3) ニーズに合った他のソリューションはありますか (ボタンをクリックすると csv が作成され、ダウンロードされ、ページが更新されます)。

手がかりを教えてください。

4

1 に答える 1

4

必要な結果を得るには、クライアントで JavaScript を使用する必要があります。ここから基本を取り入れましたhttps://stackoverflow.com/a/4168965/1002621これは、php バックエンドの例です。

クライアントで必要なコードは次のようになります。基本的な前提は、jquery を使用してダウンロード ボタンのクリック イベントをインターセプトし、トークンをフォーム ポストに挿入して、応答で送り返してファイルを知ることができるようにすることです。ダウンロードが完了したら、独自のフォーム送信などを行います。

<script type="text/javascript">

    function DownloadAndRefresh() {

        //get form and inject a hidden token input
        var form = $(this).closest('form');
        var tokenInput = $('input[name=token]');
        if (tokenInput.length == 0) {
            tokenInput = $('<input />').prop('type', 'hidden').prop('name', 'token');
            form.append(tokenInput);
        }

        //create a unqiue token we can watch for and set the hidden inputs value
        var token = new Date().getTime();
        tokenInput.prop('value', token);

        //watch for the token to come back in the documents cookie (this is set on server)
        var tokenInterval = setInterval(function () {
            //check to see if the cookie contains the token yet
            if (document.cookie.indexOf(token) != -1) {
                //submit the form again now the file has been downloaded (this causes a standard postback)
                form[0].submit();
                window.clearInterval(tokenInterval);
            }
        }, 200);

        //wait up to 60 seconds for the token then stop the interval because something probably went wrong
        setTimeout(function () { window.clearInterval(tokenInterval); }, 60000);

        //allow the current post action to continue
        return true;

    }

    $(document).ready(function () {
        //call the DownloadAndRefresh function before posting to the server
        $('#<%= DownloadButton.ClientID %>').on('click', DownloadAndRefresh);
    });

</script>

<asp:Button runat="server" ID="DownloadButton" Text="Download Button" onclick="Download_Click" /> 

サーバー コードは非常に単純で、要求フォームからトークンを取り出して応答 Cookie に入れるだけです。

    protected void Download_Click(object sender, EventArgs e)
    {
        Response.Clear();

        //set the response token cookie to be the token sent in the form request
        Response.SetCookie(new HttpCookie("token", Request.Form["token"]));
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment;filename=file.txt");
        Response.ContentType = "text/plain";
        Response.Output.Write("some text");
        Response.End();
    }
于 2013-11-07T02:39:06.703 に答える