C# asp.net上的按钮,单击“创建CSV文件”,下载并刷新页面
是否无法从页面(和数据库)收集信息、从给定信息创建csv文件、下载创建的文件并刷新页面 当前用户看到一个包含行的表,通过选中复选框选择行,按下导出按钮,CSV文件被创建并下载到用户的计算机上。问题在于页面刷新 当前,点击按钮CSV以以下方式创建和下载:C# asp.net上的按钮,单击“创建CSV文件”,下载并刷新页面,c#,asp.net,redirect,C#,Asp.net,Redirect,是否无法从页面(和数据库)收集信息、从给定信息创建csv文件、下载创建的文件并刷新页面 当前用户看到一个包含行的表,通过选中复选框选择行,按下导出按钮,CSV文件被创建并下载到用户的计算机上。问题在于页面刷新 当前,点击按钮CSV以以下方式创建和下载: //here is create csv function and then the result is outputed Response.Clear() Response.Buffer = Tr
//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) 此链接:
建议`Response.AddHeader(“Refresh”,“3;url=index.html”);那个应该会刷新页面。
这不起作用,没有重定向发生
2) 建议的另一个解决方案是创建一些额外的URL,并允许用户从给定的URL下载文件。这不适合我的情况,因为文件是根据页面上的数据创建的
3) 是否有其他适合需要的解决方案(单击按钮创建csv并下载,页面刷新)
你能给我一些线索吗?你需要在客户端使用一些javascript来获得你想要的结果。我从这里学习了一些基础知识,这是一个php后端示例 客户端上需要的代码如下所示。基本前提是使用jquery截获下载按钮单击事件,这样就可以将令牌注入表单post,然后在响应中发送回,以知道文件下载完成,然后执行自己的表单提交/任何操作
<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" />
您需要在客户端上使用一些javascript来获得所需的结果。我从这里学习了一些基础知识,这是一个php后端示例 客户端上需要的代码如下所示。基本前提是使用jquery截获下载按钮单击事件,这样就可以将令牌注入表单post,然后在响应中发送回,以知道文件下载完成,然后执行自己的表单提交/任何操作
<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" />
你不能这样做。End()中的“End”是你最大的线索。你说不可能从页面收集信息、创建csv文件、下载并刷新页面?真的不清楚你到底想要什么。是否要提供文件供下载?然后这样做,但是一旦用户完成下载,就不能将其重定向到另一个Uri。每个请求只有一个“响应”。如果您的响应类型为“文件”,您希望得到什么?我希望用户单击按钮,该按钮将根据页面上的数据创建新文件(将表格行导出为CSV),并将此文件下载到用户计算机。下载后,我需要刷新页面(这将从表中删除导出的行)。您不能这样做。End()中的“End”是你最大的线索。你说不可能从页面收集信息、创建csv文件、下载并刷新页面?真的不清楚你到底想要什么。是否要提供文件供下载?然后这样做,但是一旦用户完成下载,就不能将其重定向到另一个Uri。每个请求只有一个“响应”。如果您的响应类型为“文件”,您希望得到什么?我希望用户单击按钮,该按钮将根据页面上的数据创建新文件(将表格行导出为CSV),并将此文件下载到用户计算机。下载后,我需要刷新页面(这将从表中删除导出的行)。