When I try to Download file via ajax json MVC controller not working. not getting the MIME response. Just renders as text in window. I tried all options mentioned in forums but no luck. Please help. The controller sends the proper file back but the javascript ajax renders it as json I think. I want to get the "Save" or "Open" file response header prompt for file when I click the download button.
$("#btnDownloadEDIFile").bind("click", function (e)
{
e.preventDefault();
displayAjaxLoading(true);
$.ajax({
type: 'POST',
dataType: "json",
async: false,
contentType: 'application/json; charset=utf-8',
url: '@Url.Action(MVC.AccountsPayable.StagedInvoice.DownloadFile())',
data: JSON.stringify({ StagedFileId: $('#StagedFileId').val() , FilePath: $('#FilePath').val() , SourceFileName: $('#SourceFileName').val() }),
success: function (result) {
//alert(result);
if (result != "") {
displayNotoficationError([result]);
}
else {
}
},
error: function (result) {
//debugger;
alert(result);
displayNotoficationError([result]);
}
});
});
When I execute this it renders as text on window and also throws a ajax error in javascript.
Here's controller code
public virtual ActionResult DownloadFile(Nullable<int> StagedFileId, string FilePath, string SourceFileName)
{
string _FullPath = _WebApplicationConfiguration.SourceStagedInvoiceFileFolder + FilePath;
if (System.IO.File.Exists(_FullPath))
{
HttpContext.Response.ClearContent();
HttpContext.Response.ContentType = "application/octet-stream";
HttpContext.Response.AddHeader("content-disposition",
"attachment; filename=" + SourceFileName);
HttpContext.Response.BinaryWrite(System.IO.File.ReadAllBytes(_FullPath));
return File(_FullPath, "text/octet-stream", SourceFileName);
}
else
{
return Content("");
}
}