0

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();
    }
});
campbelt
  • 1,573
  • 5
  • 27
  • 43
  • How do you submit file to your Action? – bobek Feb 04 '14 at 19:31
  • Hmmm ... I'm not sure I understand the question. My Action is of the FileResult type. In the Action, I generate the PDF via a third-party PDF library, and then send the memory stream as the result via the following "return this.File(memoryStream.ToArray(), "application/pdf", "GridPDFExport.pdf");" This works fine, I just need to provide visual feedback that it is running. – campbelt Feb 04 '14 at 19:45
  • 1
    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 IT. – bobek Feb 04 '14 at 19:50
  • bobek, that worked fine. If you reply as an answer, I'll mark it as such. – campbelt Feb 04 '14 at 22:08

1 Answers1

0

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.

bobek
  • 8,003
  • 8
  • 39
  • 75