在动态创建PDF之前显示加载屏幕

Pio*_*nom 4 c# pdf asp.net-mvc

我有一个视图,而不是返回a View(),返回动态创建的PDF,然后在新选项卡中显示PDF.我不是在任何地方保存PDF或将其存储在任何地方.我想要做的是在创建PDF时显示加载屏幕.可以这样做吗?

public ActionResult SolicitorActionReport_Load(SolicitorActionParamsViewModel viewModel) {
    var cultivationModel = new CultivationModel(viewModel, ConstituentRepository, CampaignRepository);
    var cultivationData = cultivationModel.GetCultivationActivityData();
    var reportParamModel = new List<ReportParamModel>
                                   {new ReportParamModel {AgencyName = SelectedUserAgency.AgencyName, StartDate = viewModel.StartDate, EndDate = viewModel.EndDate}};

    var reportToRun = "ActionDateCultivationReport";
    if (viewModel.SortActionBy == SolicitorActionReportSortType.Constituent) {
        reportToRun = "ConstituentCultivationReport";
    } else if (viewModel.SortActionBy == SolicitorActionReportSortType.Solicitor) {
        reportToRun = "SolicitorCultivationReport";
    }

    return FileContentPdf("Constituent", reportToRun, cultivationData, reportParamModel, new List<FundraisingAppealMassSummary>(), new List<FundraisingAppealPortfolioSummary>());
}



public FileContentResult FileContentPdf(string folder, string reportName, object dataSet,object reportParamModel,object appealMassDataSet, object appealPortfolioDataSet) {
    var localReport = new LocalReport();
    localReport.ReportPath = Server.MapPath("~/bin/Reports/" + folder + "/rpt" + reportName + ".rdlc");
    var reportDataSource = new ReportDataSource(reportName + "DataSet", dataSet);

    var reportParamsDataSource = new ReportDataSource("ReportParamModelDataSet", reportParamModel);
    var reportParamsDataSourceMass = new ReportDataSource("FundraisingAppealMassSummaryDataSet", appealMassDataSet);
    var reportParamsDataSourcePortfolio = new ReportDataSource("FundraisingAppealPortfolioSummaryDataSet", appealPortfolioDataSet);

    #region Setting ReportViewControl

    localReport.DataSources.Add(reportDataSource);
    localReport.DataSources.Add(reportParamsDataSource);
    localReport.DataSources.Add(reportParamsDataSourceMass);
    localReport.DataSources.Add(reportParamsDataSourcePortfolio);

    localReport.SubreportProcessing += (s, e) => { e.DataSources.Add(reportDataSource); };

    string 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             
    string deviceInfo = "<DeviceInfo><OutputFormat>PDF</OutputFormat></DeviceInfo>";
    Warning[] warnings;
    string[] streams;
    byte[] renderedBytes;
    //Render the report             
    renderedBytes = localReport.Render(reportType, deviceInfo, out mimeType, out encoding, out fileNameExtension, out streams, out warnings);

    #endregion

    return File(renderedBytes, mimeType);
}
Run Code Online (Sandbox Code Playgroud)

Car*_*all 5

我不是在任何地方保存PDF或将其存储在任何地方.我想要做的是在创建PDF时显示加载屏幕.可以这样做吗?

简答

,不在新标签中.


你要做的主要问题是你在控制浏览器方面缺乏力量.具体来说,当您告诉锚点在新选项卡中打开其超链接时(即target="_blank").围绕这种方法存在一些愚蠢的方法,通常会让用户感到沮丧,因为您正在改变他们可能依赖/依赖的行为.

解决方法

通过使用此jQuery文件下载插件(查看演示),您可以非常接近您期望的结果.基本上,它操纵一个iframe队列下载.这使得可以显示加载,div同时还将用户保持在活动页面上(不将其指向另一个选项卡).然后,用户可以单击下载的PDF,该PDF最有可能在新选项卡中打开(此处查看兼容的浏览器).


如果您决定使用此插件,请执行以下步骤:

  1. 下载插件js源并将其包含在您的Scripts.
  2. 包括FileDownloadAttribute插件MVC Demo中提供的类:

    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)] 
    public class FileDownloadAttribute: ActionFilterAttribute
    {
        public FileDownloadAttribute(string cookieName = "fileDownload", string cookiePath = "/")
        {
            CookieName = cookieName;
            CookiePath = cookiePath;
        }
    
        public string CookieName { get; set; }
    
        public string CookiePath { get; set; }
    
        /// <summary>
        /// If the current response is a FileResult (an MVC base class for files) then write a
        /// cookie to inform jquery.fileDownload that a successful file download has occured
        /// </summary>
        /// <param name="filterContext"></param>
        private void CheckAndHandleFileResult(ActionExecutedContext filterContext)
        {
            var httpContext = filterContext.HttpContext;
            var response = httpContext.Response;
    
            if (filterContext.Result is FileResult)
                //jquery.fileDownload uses this cookie to determine that a file download has completed successfully
                response.AppendCookie(new HttpCookie(CookieName, "true") { Path = CookiePath });
            else
                //ensure that the cookie is removed in case someone did a file download without using jquery.fileDownload
                if (httpContext.Request.Cookies[CookieName] != null)
                {
                    response.AppendCookie(new HttpCookie(CookieName, "true") { Expires = DateTime.Now.AddYears(-1), Path = CookiePath });
                }
        }
    
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            CheckAndHandleFileResult(filterContext);
    
            base.OnActionExecuted(filterContext);
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)

    github来源

  3. FileDownload属性应用于您的ActionResult方法:

    [FileDownload]
    public ActionResult SolicitorActionReport_Load(SolicitorActionParamsViewModel viewModel) {
        ...
    
        return FileContentPdf("Constituent", reportToRun, cultivationData, reportParamModel, new List<FundraisingAppealMassSummary>(), new List<FundraisingAppealPortfolioSummary>());
    }
    
    Run Code Online (Sandbox Code Playgroud)
  4. View您要链接到报告的链接中包含必要的标记:

    <a class="report-download" href="/Route/To/SolicitorActionReport">Download PDF</a>
    
    Run Code Online (Sandbox Code Playgroud)
  5. 将事件处理程序附加到report-download锚点:

    $(document).on("click", "a.report-download", function () {
        $.fileDownload($(this).prop('href'), {
            preparingMessageHtml: "We are preparing your report, please wait...",
            failMessageHtml: "There was a problem generating your report, please try again."
        });
        return false; //this is critical to stop the click event which will trigger a normal file download!
    });
    
    Run Code Online (Sandbox Code Playgroud)

您可以在http://jqueryfiledownload.apphb.com/上查看工作演示.还有一个演示使用预先设置的jQuery UI模式来" 美化 "用户体验.

您还可以从johnculviner/jquery.fileDownload github 下载演示ASP.NET MVC解决方案,以查看所有这些工作.