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.