Click here to Skip to main content
15,667,536 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I do not know how to achieve to download file from server using web method. I tried something like this. 
I passed data and file name from angularJS to my web method, it creates excel file and upload in server. 
after completion of file creation and upload in server i returned url to another webform on success call to my JS function. 
From there I tried to open new web form where i am trying to implement code to download file. But how would my code will know which life to 
download since there could be multiple file presented. how so i pass fileName as well so that i could identified the file to download.

Please check my code and let me know what to change and what to do ? or anyother way to achieve this. 
Thank you.

What I have tried:

        public static string ExportExcel(List<Student> students, string fileName)
                    ListtoDataTableConverter converter = new ListtoDataTableConverter();

                    DataTable dt = converter.ToDataTable(students);
                    DataSet ds = new DataSet();

                    string FilePath = fileLocation + fileName + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".xlsx"; ;

                    DataSetToExcel.ExportDataSet(FilePath, ds);

                    HttpContext.Current.Response.ContentType = "application/octet-stream";
                    HttpContext.Current.Response.AppendHeader("Content-Disposition", "filename=" + Path.GetFileName(FilePath));
                    return JsonConvert.SerializeObject(new { Success = true, redirectUrl = VirtualPathUtility.ToAbsolute("~/DownloadExcelForm.aspx") });


  $scope.SendFile = function (filteredItems) {
        var xlFile = $scope.xlFilename;
        var students = [];
        students = filteredItems ;
            method: "POST",
            url: "ReportStudent.aspx/ExportExcel",
            data: { students: students, fileName: xlFile },
            contentType: "application/json",
            dataType: "json"
        }).then(function (response) {
            var responsedata = JSON.parse(;
            window.location.href = responsedata.redirectUrl;

Webform Pageload:
 protected void Page_Load(object sender, EventArgs e)
            string filepath = "";
            Response.ContentType = ContentType;
          //  Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(FilePath)); how to pass this file path ????
                if (File.Exists(filepath))
Updated 2-Aug-17 7:17am

1 solution

public byte[] DownloadFile(string FName)
    System.IO.FileStream fs1=null;
    byte[] b1=new byte[fs1.Length];
    return b1; 

Use the following reference code, hopefully it will helpful for you.
Share this answer
[no name] 2-Aug-17 13:24pm    
I achieved my passing filename in query string.
[no name] 2-Aug-17 13:25pm    
Thank you for reaching out to help me. I would try the code reference as well and let you know.
[no name] 2-Aug-17 13:47pm    
Welcome and please don't forget to mark my good rating. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900