gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 01:04:42 26 4
gpt4 key购买 nike

我有一个 View ,即返回动态创建的 PDF,然后在新选项卡中显示 PDF,而不是返回 View()。我不会在任何地方保存 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);
}

最佳答案

I'm not saving the PDF anywhere, or storing it anywhere. What I would like to do is have a loading screen show up while the PDF is being created. Can this be done?

简答

,不在新标签中。


您尝试做的事情的主要问题是您在控制浏览器方面缺乏能力。具体来说,当您告诉 anchor 在新选项卡中打开其超链接时(即 target="_blank")。有一些绕过这个问题的 hacky 方法通常只会让您的用户感到沮丧,因为您正在改变他们可能依赖/依赖的行为。

解决方法

使用此 jQuery File Download plugin 可以非常接近您想要的结果(view a demo)。基本上,它操纵一个 iframe 来排队下载。这使得显示正在加载的 div 成为可能,同时还能让用户保持在事件页面上(而不是将他们定向到另一个选项卡)。然后,用户可以单击最有可能在新选项卡中打开的已下载 PDF(查看兼容的浏览器 here)。


如果您决定使用此插件,请按照以下步骤进行应用:

  1. Download插件 js 源并将其包含在您的 Scripts 中。
  2. 包含插件 MVC 演示中提供的 FileDownloadAttribute 类:

    [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 source

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

    [FileDownload]
    public ActionResult SolicitorActionReport_Load(SolicitorActionParamsViewModel viewModel) {
    ...

    return FileContentPdf("Constituent", reportToRun, cultivationData, reportParamModel, new List<FundraisingAppealMassSummary>(), new List<FundraisingAppealPortfolioSummary>());
    }
  4. 在您将链接到报告的 View 中包含必要的标记:

    <a class="report-download" href="/Route/To/SolicitorActionReport">Download PDF</a>
  5. 将事件处理程序附加到 report-download anchor :

    $(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 模式来“美化”用户体验。

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

关于c# - 在动态创建 PDF 之前显示加载屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22260308/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com