0

I'm trying to get a .csv file with some data filtered by the jlst object (which contains the selected ids of some dropdownboxes). Here is the ajax call:

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

        $('#progress').show();
        $.ajax({
            url: '/Reports/GetPageAccessLevelsCSV?obj=' + jlst,
            dataType: "json",
            type: "GET",
            success: function (data) {
                $('#progress').hide();
            }
        });
    });

And here's the function:

    public ActionResult GetPageAccessLevelsCSV(string obj)
    {
        JavaScriptSerializer jss = new JavaScriptSerializer();
        ReportsSelectedItems cParams = jss.Deserialize<ReportsSelectedItems>(obj);

        var locationIdFilter = (cParams.LocationId != 0) ? cParams.LocationId : 0;
        var divisionIdFilter = (cParams.DivisionId != 0) ? cParams.DivisionId : 0;

        IList<UserProfile> userProfiles = new List<UserProfile>();
        userProfiles = Session.Query<UserProfile>().Where(n => n.Employee != null && ((n.Employee.Division.Id == cParams.DivisionId || cParams.DivisionId == 0) && (n.Employee.Location.Id == cParams.LocationId || cParams.LocationId == 0))).OrderBy(n => n.Employee.Name).ToList();

        MemoryStream output = new MemoryStream();
        StreamWriter writer = new StreamWriter(output, Encoding.UTF8);

        writer.Write("Name,");
        writer.Write("Team");

        foreach(UserProfile user in userProfiles)
        {
            writer.Write(user.Employee.Name);
            writer.Write(",");
            writer.Write(HasAccessToModule((int)ModuleEnum.Team, user.Id).ToString());
            writer.WriteLine();
        }

        writer.Flush();
        output.Position = 0;

        return File(output, "application/x-ms-excel", "test.csv");
    }

I was following the breakpoints and the file seems to be successfully generated. It's just that I don't know how to display the file after receiving it in the ajax call. I presume there has to be a way to handle the file (in the success event maybe). If so, how can I do that? Thanks.

Toonsylvania
  • 453
  • 6
  • 21
  • If you want to handle the file as a "file download" (as if you would download a file with your browser) then this is a quite popular question with a lot of duplicates on SO. E.g. check this: http://stackoverflow.com/questions/4545311/how-to-download-a-file-by-jquery-ajax – Tz_ Sep 04 '12 at 21:14
  • Thanks for the reply. Yes, I want it as a "file download" but that doesn't really answer my question. There is one comment in there that says this can't be done through Ajax. Either way, the difference between that example and mine is that my file is not physically stored in one particular place, it is generated on the fly. Also, I just tried the error event and I get the error "200, SyntaxError: Unexpected token N". That's probably because I would have to convert the file (into binary?) and send back the stream? – Toonsylvania Sep 05 '12 at 07:53
  • 1
    I think it does answer your question. The problem is that you cannot call a function like "I have some content here in a Javascript variable, show it to the user as a file downloaded", because that is not supported. You could get the result from the server like this, but you should tell jQuery to not parse it. Then you will have the raw content in a "variable" in the success callback. Then you will face the problem that you cannot "give it to the user as file". You can render the result on the screen as HTML DOM, but for that you would not return an excel file. – Tz_ Sep 05 '12 at 08:38
  • Thanks. You're right, what I wanted to do wasn't supported indeed. So I went for using a regular "html.beginform" (using the mvc3 helper) with 2 hidden fields auto-populated by javascript at the document ready event (since this is a partial view and the dropdownboxes are in another form of the main view). Having 2 populated fields I can now pass a viewmodel to the controller. I know it's a hack but it does what it's supposed to do. – Toonsylvania Sep 05 '12 at 09:04
  • 1
    Yes, the point is that you have to post back the entire form (the page) and then the browser will handle the result (file attachment) as download. Alternatively you can open a popup window in the browser with JS that points to the file URL. – Tz_ Sep 05 '12 at 09:46

0 Answers0