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.

Rohan Sampat
  • 930
  • 1
  • 13
  • 30

2 Answers2

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.

Prashanth
  • 98
  • 1
  • 7
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.

Community
  • 1
  • 1
Ilya Chernomordik
  • 27,817
  • 27
  • 121
  • 207