Javascript 在asp.net回发上隐藏加载图像以下载文件
我的asp.net页面上有一个按钮,可以执行回发、创建Excel文件、清除响应流并写入文件。然后,用户可以在浏览器的标准对话框中打开或保存该文件 这非常有效,我的代码基于以下内容: 由于要创建的文件需要相当长的时间,所以我创建了一个加载面板,只是一个隐藏的DIV,并在单击按钮时将其设置为可见 但我的问题是如何在导出完成后隐藏这个DIV?我就是找不到办法。我需要一个事件,当文件完全传输到浏览器时触发 这可能吗?非常感谢您的帮助 谢谢Javascript 在asp.net回发上隐藏加载图像以下载文件,javascript,asp.net,html,events,dom,Javascript,Asp.net,Html,Events,Dom,我的asp.net页面上有一个按钮,可以执行回发、创建Excel文件、清除响应流并写入文件。然后,用户可以在浏览器的标准对话框中打开或保存该文件 这非常有效,我的代码基于以下内容: 由于要创建的文件需要相当长的时间,所以我创建了一个加载面板,只是一个隐藏的DIV,并在单击按钮时将其设置为可见 但我的问题是如何在导出完成后隐藏这个DIV?我就是找不到办法。我需要一个事件,当文件完全传输到浏览器时触发 这可能吗?非常感谢您的帮助 谢谢 AJ我会做什么,长话短说: 当用户单击“下载”按钮时,使用AJ
AJ我会做什么,长话短说:
Path.GetTempFileName
。下面是一个可以在ashx处理程序中编写的示例:
public class Handler1 : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
string fName = Path.GetTempFileName();
context.Response.ContentType = "text/plain";
try
{
// Generate the Excel document
GenerateExcelInFile(fName);
// Store the file name in session for later use
context.Session["ExcelGeneratorFileName"] = fName;
// Send confirmation to the client
context.Response.Write("ok");
}
catch (Exception e)
{
context.Response.Write("error");
// TODO : Do some logging
}
}
// SNIP : IsReusable
}
public class Handler2 : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/excel";
// Make sure the browser will show a "save as" dialog to the user
context.Response.AddHeader("content-disposition", "attachment; filename=Export.pdf");
string fName = context.Session["ExcelGeneratorFileName"] as String;
if (fName != null && File.Exists(fName))
{
// Stream the excel file to the response
context.Response.WriteFile(fName);
// Remove the file
File.Delete(fName);
}
}
// SNIP : IsReusable
}
之后,使用您最喜欢的JS框架请求该处理程序,并测试返回的字符串。如果为“ok”,则调用第二部分处理程序:
public class Handler1 : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
string fName = Path.GetTempFileName();
context.Response.ContentType = "text/plain";
try
{
// Generate the Excel document
GenerateExcelInFile(fName);
// Store the file name in session for later use
context.Session["ExcelGeneratorFileName"] = fName;
// Send confirmation to the client
context.Response.Write("ok");
}
catch (Exception e)
{
context.Response.Write("error");
// TODO : Do some logging
}
}
// SNIP : IsReusable
}
public class Handler2 : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/excel";
// Make sure the browser will show a "save as" dialog to the user
context.Response.AddHeader("content-disposition", "attachment; filename=Export.pdf");
string fName = context.Session["ExcelGeneratorFileName"] as String;
if (fName != null && File.Exists(fName))
{
// Stream the excel file to the response
context.Response.WriteFile(fName);
// Remove the file
File.Delete(fName);
}
}
// SNIP : IsReusable
}
只需使用
window.location=url
即可在javascript中调用此页面。content disposition标头将告诉浏览器不应显示此URL,而应仅下载此URL,因此您的用户应留在下载页面上。我该怎么做,长话短说:
Path.GetTempFileName
。下面是一个可以在ashx处理程序中编写的示例:
public class Handler1 : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
string fName = Path.GetTempFileName();
context.Response.ContentType = "text/plain";
try
{
// Generate the Excel document
GenerateExcelInFile(fName);
// Store the file name in session for later use
context.Session["ExcelGeneratorFileName"] = fName;
// Send confirmation to the client
context.Response.Write("ok");
}
catch (Exception e)
{
context.Response.Write("error");
// TODO : Do some logging
}
}
// SNIP : IsReusable
}
public class Handler2 : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/excel";
// Make sure the browser will show a "save as" dialog to the user
context.Response.AddHeader("content-disposition", "attachment; filename=Export.pdf");
string fName = context.Session["ExcelGeneratorFileName"] as String;
if (fName != null && File.Exists(fName))
{
// Stream the excel file to the response
context.Response.WriteFile(fName);
// Remove the file
File.Delete(fName);
}
}
// SNIP : IsReusable
}
之后,使用您最喜欢的JS框架请求该处理程序,并测试返回的字符串。如果为“ok”,则调用第二部分处理程序:
public class Handler1 : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
string fName = Path.GetTempFileName();
context.Response.ContentType = "text/plain";
try
{
// Generate the Excel document
GenerateExcelInFile(fName);
// Store the file name in session for later use
context.Session["ExcelGeneratorFileName"] = fName;
// Send confirmation to the client
context.Response.Write("ok");
}
catch (Exception e)
{
context.Response.Write("error");
// TODO : Do some logging
}
}
// SNIP : IsReusable
}
public class Handler2 : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/excel";
// Make sure the browser will show a "save as" dialog to the user
context.Response.AddHeader("content-disposition", "attachment; filename=Export.pdf");
string fName = context.Session["ExcelGeneratorFileName"] as String;
if (fName != null && File.Exists(fName))
{
// Stream the excel file to the response
context.Response.WriteFile(fName);
// Remove the file
File.Delete(fName);
}
}
// SNIP : IsReusable
}
只需使用
window.location=url
即可在javascript中调用此页面。“内容处置”标题将告诉浏览器,此URL不应显示,只能下载,因此您的用户应停留在下载页面上。感谢您提供的非常详细的答案,尽管我试图不使用临时文件,但总会有一些文件落在后面。感谢您提供的非常详细的答案,不过,我尽量不使用临时文件,总是会有一些文件落在后面。