0

I have a success function which hits the server, send a list of data process it and create an excel file on the processed data. The whole code is working fine but I cannot download file. File is being created on the server.

Client side code is..

MOCService.getFilterExcel($scope.filterExcel).success(function (data, responce) { console.log(responce) return responce; }) 

Server side:

public ActionResult getFilterExcel(List<FilterExcel> data) { System.Data.OleDb.OleDbConnection conn = null; try { string filename = ""; var oldfilename = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\\datafiles\\lswm\\reports.xlsx"; filename = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\\datafiles\\report\\report" + HttpContext.User.Identity.Name + ".xls"; System.IO.File.Copy(oldfilename, filename, true); conn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename + "; Extended Properties=\"Excel 12.0 Xml;HDR=NO\";"); conn.Open(); //return null; int i = 3; foreach (var item in data) { var strsql = "insert into [Report$C" + (i) + ":L" + (i) + "] (f1,f2,f3,f4,f5,f6,f7,f8,f9,f10)values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}')"; var cmd = conn.CreateCommand(); cmd.CommandText = string.Format(strsql, item.ccrfNo, item.requestDate, item.requestedBy, item.title, item.changeImpact, item.plant, item.division, "", item.taskStatus, item.currentPhase ); var rowcount = cmd.ExecuteNonQuery(); i++; } } catch(Exception ex) { } finally { if (conn != null && conn.State == ConnectionState.Open) conn.Close(); } return File(Request.PhysicalApplicationPath + "\\datafiles\\report\\report" + HttpContext.User.Identity.Name + ".xls", "Application/MS-excel", "report" + HttpContext.User.Identity.Name + ".xls"); } 

And if I hit the action method directly from URL then the file is downloaded but via application it does not download.

15
  • You cannot download files using ajax. You need to use location.href = ... in the success callback to call a method that returns the file Commented Dec 3, 2016 at 9:19
  • is the file that you are trying to download is only excel?? or are there chances for other file type too? Commented Dec 3, 2016 at 9:21
  • Possible duplicate of Download a file by jQuery.Ajax Commented Dec 3, 2016 at 9:22
  • It is only excel file. Commented Dec 3, 2016 at 9:23
  • @StephenMuecke you can still download the file via ajax.. we just need to have a iframe and set the src to this controller action.. which will downbload the file to client machine automatically Commented Dec 3, 2016 at 9:23

2 Answers 2

0
var blob = new Blob([res.data], {type: "application/*"}); var objectUrl =(window.URL || window.webkitURL).createObjectURL(blob); var downloadLink = document.createElement("a"); document.body.appendChild(downloadLink); downloadLink.style = "display: none"; downloadLink.href = objectUrl; downloadLink.download = "Sample.xlsx"; downloadLink.click(); 

this works for me.

Sign up to request clarification or add additional context in comments.

Comments

0

The best way I know of to download files using AJAX is to use FileSaver.js. This handles lots of various tricks you would have to do yourself otherwise. So you just need to use the Blob api to get the blob itself and combine it with the FileSaver.js to save the file to disk.

P.S. There are multiple info on google and on SO (e.g. here) how to get the blob using AJAX, and then you can use FileSaver.js github page to figure out how to save that blob to disk.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.