3

I am trying to stream output from a MVC 5 controller, and am having trouble making the browser (IE 10,11, Chrome) recognize the output stream and pipe to Excel. I have verified that the stream is a valid Excel file by saving the output to a file and opening it. I have used Fiddler to determine the headers and data look ok (see below). The controller is being called by the jquery ajax call listed below. The Controller method being called is listed below as well. The ajax return status is coming back to the error function with req.status = 200 (OK), but status = 'parseerror'. Any help would be appreciated.

$.ajax({
    type: "POST",
    url: "http://localhost:42655/Home/Save",
    data: paramdata,
    contentType: defaultjsonformat,
    dataType: 'json',
    success: function(data) {
         alert(data);
    },
    statusCode: {
        404: function (content) { alert('cannot find resource'); },
        500: function (content) { alert('internal server error'); }
    },
    error: function (req, status, errorObj) {
        if (status === "timeout") {
            alert('Timeout');
        } else {
            alert(status);
        }

    }

);

HomeController.cs:

[AcceptVerbs(HttpVerbs.Post)]
public FileStreamResult Save(string document)
{
    document = document.Trim();


    byte[] bytefile = Export.Excel.ProcessExport.ExportFile(document);

    var stream = new MemoryStream(bytefile);

    return File(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", document + DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss") + ".xlsx");

}

Fiddler output:

HTTP/1.1 200 OK
Cache-Control: private
Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Server: Microsoft-IIS/8.0
X-AspNetMvc-Version: 5.2
Content-Disposition: attachment; filename=test2015-04-03-06-52-04.xlsx
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?RDpcTFJQXFJvb3QtREVWXFNjZW5hcmlvXFNjZW5hcmlvXEhvbWVcU2F2ZQ==?=
X-Powered-By: ASP.NET
Date: Fri, 03 Apr 2015 13:52:04 GMT
Content-Length: 9779

PK    6 F H  

EDIT: I realized that the ajax call is receiving all the data, so I am now wondering how to just make a call to the controller with arguments.

ekad
  • 14,436
  • 26
  • 44
  • 46
Mark McWhirter
  • 1,168
  • 3
  • 11
  • 28
  • According to http://stackoverflow.com/questions/5061310/jquery-returning-parsererror-for-ajax-request you are saying to jquery you are expecting a result of type JSON and then you are returning a file, you should remove the line dataType: 'json' – Angelo Reis Apr 03 '15 at 14:41
  • Yes, but that is not the behavior I am wanting. I would like to have the ajax call redirect to the controller, which in turn will generate a file for streaming out to the browser. If there is an alternative to the ajax call that would call the controller with parameters, that would be acceptable too. – Mark McWhirter Apr 03 '15 at 14:51
  • If you really need it to be a POST I believe you can create a form on your page and instead of making the ajax call you can just change fields on that form and submit it through JQuery and you will get the file from the server that way. If it can be a GET you can just change the location of your window and that will download the file. – Angelo Reis Apr 03 '15 at 15:02
  • The ajax call is embedded in a kendo treeview select function, so I am not sure that the form post method would be the way to go. At this point any method that can be called within the select statement with parameters to the controller is good for me. – Mark McWhirter Apr 03 '15 at 15:10

1 Answers1

0

I don't think you're going to be able to achieve what you're looking for via the jQuery ajax call. It's not designed to download files like that.

I've had good luck achieving behavior identical to yours (Returning excel spreadsheets from an MVC controller asynchronously) using the following plugin: https://github.com/johnculviner/jquery.fileDownload.

Hope that helps :)

Austin_G
  • 177
  • 11