如何在Asp.Net中使用Response.WriteFile方法下载文件后进行回发 [英] How to do postback after downloading File using Response.WriteFile method in Asp.Net
问题描述
我正在使用Response.WriteFile方法将Excel工作表从所需位置下载到客户端机器。下面是代码: -
I am using Response.WriteFile method for downloading an excel sheet from a desired location to client machine.Below is the code:-
// Checking if file exists
if (myfile.Exists)
{
// Clear the content of the response
HttpContext.Current.Response.ClearContent();
// Add the file name and attachment, which will force the open/cancel/save dialog box to show, to the header
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + myfile.Name);
// Add the file size into the response header
HttpContext.Current.Response.AddHeader("Content-Length", myfile.Length.ToString());
// Set the ContentType
HttpContext.Current.Response.ContentType = ReturnExtension(myfile.Extension.ToLower());
// Write the file into the response (TransmitFile is for ASP.NET 2.0. In ASP.NET 1.1 you have to use WriteFile instead)
HttpContext.Current.Response.WriteFile(myfile.FullName);
// End the response
HttpContext.Current.Response.End();
}
private string ReturnExtension(string fileExtension)
{
switch (fileExtension)
{
case ".xlsx":
return "application/excel";
default:
return "application/octet-stream";
}
}
我想要一旦下载弹出窗口出现并要求保存/打开/取消并且最终用户选择他的选项然后在完成该任务之后,回调发生在调用它的网页上。假设用户单击保存然后保存文件,并且在保存帖子后将会发生。如果用户选择打开,则会打开该文件,然后在调用此下载功能的网页上发回帖子。
I want that once the download pop up will appear and asking for Save/Open/Cancel and the end user select his option then after completion of that task a post back happen to the webpage from where it is been invoked. Suppose if user click save then the file get saved and after saving a post back will happen.Same if user select open then the file will be opened and then a post back happen to the webpage from where this Download function is invoked.
推荐答案
这是非常容易的人你可以添加带有visible =false属性的按钮和
OnClick =button1_Click属性
it is very easy man you can add button with visible = "false" property and with
OnClick="button1_Click" property
button1_Click((object sender, System.EventArgs e)
{
//and leave it empty;
}
在你完成代码结束后调用此按钮事件
如下:
the after you finish your code at the end call this button event
as follows:
button1_Click(null,null);
ex:正如你所说的,如果用户选择open,那么文件将被打开,然后发回一个帖子就可以了这个代码的结尾(和打开的文件)调用空函数...简单
关于
ex: as you say if user select open then the file will be opened and then a post back happen ok at the end of this code (and the file opened)call the empty function...easy
regard
我有一个同样的问题PLZ帮助我
解决方案1不起作用
i have a same problem plz help me
solution1 does''t work
< pre lang =c# >< pre lang =text>< pre lang =text>< / pre>< / pre>< / pre>
All You可以。在导出Excel之前,您应该使用JavaScript方法。调用服务器端事件。或者,如果你可以在JavaScript中处理它会更好..
这篇关于如何在Asp.Net中使用Response.WriteFile方法下载文件后进行回发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!