【问题标题】:asp.net on button click create CSV file, download it and refresh pageasp.net 上的按钮单击创建 CSV 文件,下载并刷新页面
【发布时间】:2013-11-18 05:03:13
【问题描述】:

是否无法从页面(和数据库)收集信息、从给定信息创建 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) 此链接: Redirecting to another page after Response.End() has been called in C#

建议 `Response.AddHeader("Refresh", "3; url=index.html");那应该刷新页面。 这不起作用,不会发生重定向。

2) 建议的另一个解决方案是创建一些额外的 URL 并允许用户从给定的 URL 下载文件。这不适合我的情况,因为 File 是根据页面上的数据创建的。

3)有没有其他适合需求的解决方案(按钮点击创建csv并下载,页面刷新)?

请给我一些线索好吗?

【问题讨论】:

  • 你不能这样做。 Response.End() 中的“结束”是你最大的线索。
  • 你说不可能从页面收集信息,创建csv文件,下载并刷新页面?
  • 真的不清楚你到底想要什么。您要提供文件以供下载吗?然后执行此操作,但是一旦用户完成下载,您就无法将用户重定向到另一个 Uri。
  • 每个请求只有一个“响应”。如果您的回复类型是“文件”,您期望什么?
  • 我希望用户单击按钮,这将从页面上的数据创建新文件(将表行导出为 CSV)并将此文件下载到用户计算机。下载后我需要刷新页面(这将从表中删除导出的行)。

标签: c# asp.net redirect


【解决方案1】:

您将需要在客户端上使用一些 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();
    }

【讨论】:

    猜你喜欢
    • 2012-02-17
    • 2017-05-17
    • 2014-10-21
    • 1970-01-01
    • 2013-10-13
    • 2014-04-10
    • 1970-01-01
    • 1970-01-01
    • 2015-07-05
    相关资源
    最近更新 更多