-2

OK, this sounds dumb even to me, but I am clearly having a "bad brain day" and need help.

On a button click, I want to generate a file based on parameters taken from 2 ViewData fields and a checkbox control, and have the file be downloaded/displayed, just as you get with a fixed link.

Most of it is going fine, the controller method passes back a file like so: Return File(filePath, "text/csv") - but then what? How do I connect that to the button and have the file download/open dialog come up?

I feel I am missing something really simple. Just calling the controller via ajax seems to do nothing... the code is called but I see no results

***** EDIT: ******

The following gets me a file automatically downloaded, but with the name "download" - I want to offer the user the choice to open or download, and to set the filename - how do I do that?

serialize = function (obj) {
    var str = [];
    for (var p in obj)
        if (obj.hasOwnProperty(p)) {
           str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
        }
    return str.join("&");
};

 var params = {
    ID1: '@ViewData("ID1")',
    ID2: '@ViewData("ID2")',
    flag: getFlag()
 };

 var actionUrl = ('@Url.Action("ProduceReport", "Report")');
    actionUrl += "/?" + serialize(params);

 window.open(actionUrl);

* 2nd edit * Controller code - this produces a file and returns the path. After the call to ProduceReport The file is there on that path, this I have checked. It is used in production to email the file (works fine).

public FileResult ProduceReport(int ID1, int ID2, bool flag = false)
{
    var filePath = ExcelReports.ProduceReportExcel(Models.UserInfo.GetCurrentUserID, ID1, ID2, flag);        
    return File(filePath,"application/vnd.ms-excel");
}
kpollock
  • 3,899
  • 9
  • 42
  • 61
  • 1
    You cannot download a file using ajax. You need to redirect to a method that returns a `FileResult` (refer [Download Excel file via AJAX MVC](https://stackoverflow.com/questions/16670209/download-excel-file-via-ajax-mvc) –  Mar 08 '18 at 11:58
  • 1
    Downloading via ajax makes no sense - an ajax call returns data as a Javascript variable. Putting a file intended for download in there is useless, it needs to go to the user's disk via a standard HTTP request. Just make a hyperlink which opens in a new tab and points to the URL of the controller method which returns the file. Or use window.open() if you want to trigger it programatically. – ADyson Mar 08 '18 at 12:38
  • My controller method returns a FileResult, it is what to do from there that escapes me... – kpollock Mar 08 '18 at 12:45
  • 1
    Downvoters? Why? What is wrong with the question and how could I improve it? I did not include a lot of code because it is in a big app that is highly modularised, I would need to list a lot of functions - if that's what people want to see, then fine.I can do that. I did add the javascript. – kpollock Mar 08 '18 at 13:10
  • 1
    Possibly because there are endless examples of file download using MVC online already? If you want to set the filename, you have to set the relevant header. You haven't shown us your controller code so we don't know what you're doing wrong. (That's another good reason to downvote, we can't fix what we can't see) And whether the file is downloaded automatically or the "open/save" dialog is offered is a setting in the browser, it's not up to you. Some browsers don't even have such a feature. – ADyson Mar 08 '18 at 13:47
  • https://stackoverflow.com/help/how-to-ask explains how to ask a good question. – ADyson Mar 08 '18 at 13:48
  • 1
    I have finally asked a question after a day of going through many of those answers and finding no solution that fits my needs. Is it totally verboten to ask questions in the somewhat abstract level these days? I have been on Stackoverflow for a very, very long time, and people used to be a little less harsh – kpollock Mar 08 '18 at 14:38
  • Thank you for the nugget about the browser. That is annoying. So how does one best offer that choice? A link to an example is all I am after. – kpollock Mar 08 '18 at 14:49
  • "So how does one best offer that choice?". Read my comment again - you can't control it – ADyson Mar 08 '18 at 15:47
  • 1
    You can ask abstract questions of course, but probably the reason people are harsher these days is that there are now so many questions that usually it's been answered before in one form or another. More specific questions dealing with bugs in specific implementations are generally a bit more relevant. Reference implementations of common tasks such as file downloads already exist both in SO and elsewhere. – ADyson Mar 08 '18 at 15:49

1 Answers1

0

The acceptable compromise that I found, to at least get the file downloaded. I will have to do further research to see if I can print it automatically.

Javascript:

serialize = function (obj) {
    var str = [];
    for (var p in obj)
        if (obj.hasOwnProperty(p)) {
           str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
        }
    return str.join("&");
};

 var params = {
    ID1: '@ViewData("ID1")',
    ID2: '@ViewData("ID2")',
    flag: getFlag()
 };

 var actionUrl = ('@Url.Action("ProduceReport", "Report")');
    actionUrl += "/?" + serialize(params);

 window.location.href = actionUrl;

Controller: Note 3rd parameter on the File() call

public FileResult ProduceReport(int ID1, int ID2, bool flag = false)
{
    var filePath = ExcelReports.ProduceReportExcel(Models.UserInfo.GetCurrentUserID, ID1, ID2, flag);        
    return File(filePath,"application/vnd.ms-excel",System.IO.Path.GetFileName(filePath));
}
kpollock
  • 3,899
  • 9
  • 42
  • 61