Jquery 从控制器生成/返回文件时显示覆盖
我目前正在生成一个相当大的文件,并从一个视图中单击按钮从控制器返回。 我希望能够在生成文件时显示一个覆盖图,说明“正在生成文件”,一旦生成完成,覆盖图就会消失。 我该怎么做这样的事情呢 下面是我的控制器的示例Jquery 从控制器生成/返回文件时显示覆盖,jquery,asp.net-mvc,overlay,Jquery,Asp.net Mvc,Overlay,我目前正在生成一个相当大的文件,并从一个视图中单击按钮从控制器返回。 我希望能够在生成文件时显示一个覆盖图,说明“正在生成文件”,一旦生成完成,覆盖图就会消失。 我该怎么做这样的事情呢 下面是我的控制器的示例 public ActionResult Generate(FormViewModel fvm) { var isValid = AreInputsValid(fvm); if (!isValid) { TryU
public ActionResult Generate(FormViewModel fvm)
{
var isValid = AreInputsValid(fvm);
if (!isValid)
{
TryUpdateModel(fvm);
return View("Index", );
}
RenderReport(new Report(fvm));
return View();
}
private void RenderReport(Models.Report report)
{
var localReport = new LocalReport { ReportPath = report.ReportPath };
var reportDataSource = new ReportDataSource(report.DataSourceName, report.Model);
localReport.DataSources.Add(reportDataSource);
var reportType = "PDF";
string mimeType;
string encoding;
string fileNameExtension;
//The DeviceInfo settings should be changed based on the reportType
//http://msdn2.microsoft.com/en-us/library/ms155397.aspx
var deviceInfo =
string.Format("<DeviceInfo><OutputFormat>{0}</OutputFormat><PageWidth>11in</PageWidth><PageHeight>8.5in</PageHeight><MarginTop>0.5in</MarginTop><MarginLeft>0.25in</MarginLeft><MarginRight>0.25in</MarginRight><MarginBottom>0.5in</MarginBottom></DeviceInfo>", reportType);
Warning[] warnings;
string[] streams;
//Render the report
var renderedBytes = localReport.Render(
reportType,
deviceInfo,
out mimeType,
out encoding,
out fileNameExtension,
out streams,
out warnings);
//Clear the response stream and write the bytes to the outputstream
//Set content-disposition to "attachment" so that user is prompted to take an action
//on the file (open or save)
Response.Clear();
Response.ContentType = mimeType;
Response.AddHeader("content-disposition", "attachment; filename=" + report.ReportName + "." + fileNameExtension);
Response.BinaryWrite(renderedBytes);
Response.End();
}
公共行动结果生成(FormViewModel fvm)
{
var isValid=AreInputsValid(fvm);
如果(!isValid)
{
TryUpdateModel(fvm);
返回视图(“索引”);
}
渲染报告(新报告(fvm));
返回视图();
}
专用void渲染报告(Models.Report)
{
var localReport=newlocalreport{ReportPath=report.ReportPath};
var reportDataSource=新的reportDataSource(report.DataSourceName,report.Model);
localReport.DataSources.Add(reportDataSource);
var reportType=“PDF”;
字符串模拟类型;
字符串编码;
字符串文件名扩展名;
//应根据报告类型更改DeviceInfo设置
//http://msdn2.microsoft.com/en-us/library/ms155397.aspx
var设备信息=
格式(“{0}11in8.5in0.5in0.25in0.25in0.5in”,reportType);
警告[]警告;
字符串[]流;
//提交报告
var renderedBytes=localReport.Render(
报告类型,
deviceInfo,
输出mimeType,
输出编码,
输出文件名扩展名,
流出的溪流,
发出警告);
//清除响应流并将字节写入outputstream
//将内容处置设置为“附件”,以便提示用户采取操作
//在文件上(打开或保存)
Response.Clear();
Response.ContentType=mimeType;
Response.AddHeader(“内容处置”、“附件;文件名=“+report.ReportName+””+文件名扩展名);
BinaryWrite(renderBytes);
Response.End();
}
提前感谢我将使用jquery显示覆盖,并在div中显示您的“生成文件”消息。您可以单击onClientClick弹出此图标,它将一直保留,直到您从服务器返回
我总是把这段代码放在我使用blockUI的页面上
当出于某种原因您必须从服务器返回并显示模式时,它会很有用,否则它将隐藏以前可见的模式
function pageLoad(event, args)
{
var hdf = $('[id$=hdf_DisplayModal]').val();
if(hdf != "")
showPopup(hdf);
else
$.unblockUI();
}
然后,我有一个附加的外部jasvascript文件,其中包含以下内容:
function showPopup(modal)
{
showPopup(modal, null);
}
function showPopup(modal, fadeInTime)
{
if(fadeInTime == null)
fadeInTime = 500;
modal = $('[id$=' + modal + ']');
$.blockUI({ message: modal,
fadeIn : fadeInTime,
css: {
top: ($(window).height() - modal.height())/2 + 'px',
left: ($(window).width() - modal.width()) /2 + 'px',
width: modal.width() + 'px'
}
});
}
function closePopup(modal)
{
closePopup(modal, null);
}
function closePopup(modal, fadeOutTime)
{
$('[id$=hdf_DisplayModal]').attr("value", "");
modal = $('[id$=' + modal + ']')
$.unblockUI({ message: modal,
fadeOut: fadeOutTime
});
}
现在我从来没有在MVC中做过这些,但从我的同事那里听说,经过一点调整,所有这些都应该是可能的。我希望它能帮上忙。用户界面是您遇到的最小问题 如果您有一个需要几秒钟才能执行的操作,那么您将占用ASP.NET工作线程来破坏站点的可伸缩性。这就是为什么。还有 此外,您的操作不应写入
响应
。这样做会使操作不必要地难以测试,并偏离标准的MVC管道,其中像ViewResult
这样的结果对象会实际写入响应
最后,当您准备好更新UI时,通常会通过回调来完成
例如,从“在原始视图中加载消息:
<div id="report">Loading...</div>
我尝试过这个方法,但一旦文件返回,页面仍会显示div。我将使用控制器中的代码更新我的帖子。如果您尝试调用$.unbui()可能会有所帮助;当您从服务器返回时?我编辑了我的答案,提供了更多信息。很抱歉,我仍然不理解某些内容。如果写入响应流是一个“否”,那么您建议我如何返回生成的文件?我是mvc新手,因此希望您提供帮助。一种方法是编写自定义结果类型。有一个sa这里的示例:一般的想法是操作将数据传递给结果,结果更新响应。
$(document).ready(function() {
$("#report").load("/path/to/action");
});