mvc4.0 html.actionlink comfired,MVC 4:如何在FileResult Action生成PDF以发送回浏览器时创建可视指示器?(MVC 4: How to create...

MVC 4:如何在FileResult Action生成PDF以发送回浏览器时创建可视指示器?(MVC 4: How to create visual Indicator while FileResult Action generates PDF to be sent back to browser?)

我有FileResult控制器动作,在某些情况下可能需要几分钟才能运行。 我想向我们的用户提供Action正在运行的可视指示,例如消息,微调器或进度条。 麻烦的是,我很难搞清楚如何在前端“检测”动作已经完成。

我一直在玩Ajax对FileResult Action的调用,但这不起作用,因为Ajax无法将文件返回给浏览器。 我还查看了异步操作和任务,但看起来FileResult不支持“await”关键字,我认为这是必要的。

在这一点上,我不知道下一步该尝试什么,并且对思想/想法非常开放。

编辑:

我的工作是bobek建议的。 基本上,我将PDF文件保存到磁盘,并将文件路径作为ActionResult内容(fullFilePath)返回。 然后,在我的页面上,我使用类似这样的东西在Action执行时显示一个小旋转器gif,然后在完成后显示文件的链接。

var img = $("img#reportLoadingIcon");

var link = $('a#exportedFileLink');

link.hide();

img.show();

$.ajax({

url: url,

success: function (result) {

link.attr({target: '_blank', href: result});

link.show();

img.hide();

}

});

I have FileResult Controller Action that can take a couple minutes to run under some circumstances. I would like to provide our users a visual indication that the Action is running, such as a message, a spinner, or a progress bar. The trouble is, I'm having a tough time figuring out how to "detect" on the front end that the Action has completed.

I have been toying with Ajax calls to the FileResult Action, but that doesn't work because Ajax can't return a file to the browser. I also looked at Asynchronous Actions and Tasks, but it looks like FileResult does not support the "await" keyword, which I think is necessary.

At this point, I don't know what to try next, and am very open to thoughts/ideas.

Edit:

I got this working as bobek suggested. Basically, I saved the PDF file to disk, and returned the file path as an ActionResult Content(fullFilePath). Then, on my page, I used something like this to display a little spinner gif while the Action executed, and then a link to the file after it completed.

var img = $("img#reportLoadingIcon");

var link = $('a#exportedFileLink');

link.hide();

img.show();

$.ajax({

url: url,

success: function (result) {

link.attr({target: '_blank', href: result});

link.show();

img.hide();

}

});

原文:https://stackoverflow.com/questions/21560490

更新时间:2020-01-27 01:04

最满意答案

您可以将其设置为通过AJAX调用的常规POST操作,而不是返回文件,您可以返回文件路径并将用户重定向到该路径。

You could make it a regular POST action called via AJAX, and instead of returning a File you can return path to file and redirect the user to that path.

相关问答

问题:我不知道我应该如何处理这些数据以将其传送给用户。 出于这个原因,您不应该使用AJAX来下载文件。 您可以执行以下操作: 用户点击下载按钮 使用JavaScript,你会看到一些进度图像,告诉他他将不得不等待 使用JavaScript,您可以生成并向DOM中注入隐藏的iframe,其src属性指向应该生成文件的控制器操作 一旦加载iframe,您可以隐藏进度图像 Problem: I have no Idea what I'm supposed to do with this data to

...

如果您使用HttpCacheability.Private则ETag将被禁止。 你可以找到关于这个问题的更多信息 如果您将其更改为HttpCacheability.ServerAndPrivate它应该可以工作 The ETag will be suppressed if you use HttpCacheability.Private. You can find more information on this question If you change it to HttpCacheabil

...

使用您之前使用的相同代码: $(function() {

$('#exportButton').click(function() {

location.href = 'CurReport/GetCSVReport';

});

});

get触发器的Ajax调用,你不想在这里。 您使用jQuery绑定事件,但操作保持不变。 要添加查询字符串参数,请使用: location.href = 'CurReport/GetCSVReport?filter=' + escap

...

我设计了一个解决方法,以便即使异步运行也可以监视我的void函数的输出。 我创建了一个javascript window.setInterval函数,它不时检查void函数是否创建了最终输出文件zipfile。 为了监控这个,我使用了一个带有zipfile文件名的cookie。 一旦文件可读,那就是我将文件名发送到FileResult函数的时间。 var exportPhotoBatchck

exportPhotoBatchck = window.setInterval(function

...

你说你使用的是第三方的MVC扩展,它返回一个FileResult并且你想要访问包含在结果中的文件细节。 如果你看到FileResult ,它是一个抽象类,我只知道MVC中有三个内置实现(我不知道其他实现): FileContentResult , FileStreamResult和FilePathResult 。 所有这些类都提供公共属性来访问文件的详细信息。 例如。 FileContentResult包含一个名为FileContents的公共属性, FileContents文件的内容作为字节数组

...

修复它,得到一个在actionlink类上触发的jquery loadevent。 在firefox中有一个烦人的警报,chrome没有提醒我。 尝试不同的浏览器是我的建议。 Fixed it, got a jquery loadevent that fired on actionlink class. Got an annoying alert in firefox, chrome did not alert me. Try diffrent browsers is my suggestion.

...

您可以将其设置为通过AJAX调用的常规POST操作,而不是返回文件,您可以返回文件路径并将用户重定向到该路径。 You could make it a regular POST action called via AJAX, and instead of returning a File you can return path to file and redirect the user to that path.

感谢丹尼尔JG这个微小的修改完美地工作。 MemoryStream ms = new MemoryStream();

HtmlToPdf converter = new HtmlToPdf();

ViewData.Model = model;

using (var sw = new StringWriter()) {

var viewengineresult = ViewEngines.Engines.FindPartialView(this.ControllerCon

...

MVC行动: public FileResult ConvertPathInfoToPDF(PositionInfo[] pInfo)

{

MemoryStream fs = new MemoryStream();

Rectangle rec = new Rectangle(PageSize.A4);

Document doc = new Document(rec);

PdfWriter writer = PdfWriter.

...

我最近有这个完全相同的问题,我相信我的解决方案(基于对相关问题的答案 )是你正在寻找的这个问题。 基本思想是在生成文件时显示进度微调器(在我的情况下),在文件生成完成时隐藏进度微调器,然后提供要下载的文件。 要做到这一点,我需要四件事: 首先,控制器上的操作需要将文件存储在Session中 [HttpPost]

public ActionResult RunReport(ReportViewModel viewmodel)

{

// Process that generates the ob

...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值