You will not be able to use JavaScript to save the file to disk, because it is locked for security reasons.
An alternative would be to save the file in the FinaliseQuote action FinaliseQuote (and return only the file identifier), and then create another action method that responds to the GET request and returns the file. In your success function, you set window.location.href to point to a new action method (you need to pass the file identifier). Also make sure that the MIME type is set to application/octet-stream and this will force the browser to download the file.
Controller:
[HttpPost] public JsonResult FinaliseQuote(string quote) { var finalisedQuote = JsonConvert.DeserializeObject<FinalisedQuote>(System.Uri.UnescapeDataString(quote)); // save the file and return an id... } public FileResult DownloadFile(int id) { var fs = System.IO.File.OpenRead(Server.MapPath(string.Format("~/Content/file{0}.pdf", id))); // this is needed for IE to save the file instead of opening it HttpContext.Response.Headers.Add("Content-Disposition", "attachment; filename=\"filename\""); return File(fs, "application/octet-stream"); }
JS:
success: function (data) { window.location.href = "/NewQuote/DownloadFile?id=" + data; },
source share