Show loading screen before dynamically created PDF file

I have a view that, instead of returning, View()returns a dynamically generated PDF file and then shows the PDF file in a new tab. I do not save a PDF file anywhere or do not save it anywhere. What I would like to do is show the loading screen while creating the PDF file. It can be done?

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);
}
+4
source share
2 answers

I do not save a PDF file anywhere or do not save it anywhere. What I would like to do is show the loading screen while creating the PDF file. It can be done?

Short answer

No , not in a new tab.


, , , . , , (.. target="_blank"). , , , /.

, jQuery ( ). , iframe, . div, ( ). PDF , (. ).


, :

  • Download js Scripts.
  • FileDownloadAttribute, MVC:

    [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);
        }
    }
    

    github

  • FileDownload ActionResult:

    [FileDownload]
    public ActionResult SolicitorActionReport_Load(SolicitorActionParamsViewModel viewModel) {
        ...
    
        return FileContentPdf("Constituent", reportToRun, cultivationData, reportParamModel, new List<FundraisingAppealMassSummary>(), new List<FundraisingAppealPortfolioSummary>());
    }
    
  • View, :

    <a class="report-download" href="/Route/To/SolicitorActionReport">Download PDF</a>
    
  • 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!
    });
    

http://jqueryfiledownload.apphb.com/. , jQuery UI-, "" .

ASP.NET MVC johnculviner/jquery.file github, .

+5

, :

  • GIF-, PDF ( , PDF - , )

  • iFrame: iframe. GIF , iFrame PDF. : iframe 100%
0

Source: https://habr.com/ru/post/1530675/


All Articles