Click here to Skip to main content
14,773,437 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I need to download an excel from clientside. Unfortunately none of the jquery plugins or methods allow you to download file by passing Headers in the request. I definitely have to pass headers in request and there is not question about it. So what i did was, i created a web service method and made an ajax call from jquery. In the webservice, i was able to create headers of my choice and then, i was able to make a request to the service layer. i get the response as HttpWebResponse and am able to write the response to the stream. The issue is, Nothing happens. The file is not downloaded or anything happens. when i debug, there are no errors. Please let me know if i am missing anything.

   public void PostExcel(ExcelDto searchDto)
       var request =  (HttpWebRequest)WebRequest.Create("url");
       request.ContentType = "text/json";
       request.Method = "POST";
       request.Headers.Add("X-Header1-Context", "abc");
       request.Headers.Add("X-Header2-Scope", "XY");
       request.Credentials = new NetworkCredential("uname", "pwd", "domain");
       request.Headers.Add("X-Header3", "PQR");
       using (var streamWriter = new StreamWriter(request.GetRequestStream()))

       HttpWebResponse httpResponse = (HttpWebResponse)request.GetResponse();
       ForceDownload(httpResponse, "test.xlsx");

The Force Download method is where i download the file:

public void ForceDownload(HttpWebResponse fileResp, string fileName)
       Stream stream = null;
        int bytesToRead = 10000;
        byte[] buffer = new Byte[bytesToRead];
           stream = fileResp.GetResponseStream();
           var resp = HttpContext.Current.Response;
           resp.ContentType = "application/application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
           resp.AddHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
           resp.AddHeader("Content-Length", fileResp.ContentLength.ToString());
           int length;
                    length = stream.Read(buffer, 0, bytesToRead);
                    resp.OutputStream.Write(buffer, 0, length);
                    buffer = new Byte[bytesToRead];
            } while (length > 0); //Repeat until no data is read
            if (stream != null)


1 solution

You actually didn't really need to create a WebService to perform an action to just let the client download the file. What you would do, is to create a hyperlink for the file link (generally to the page) from where the user would download the file.

<a href="~/DownloadFile?FileName=@Filename">Download</a>
<!-- Where Filename will be populated by a server-side variable
     Can be anything, but would be used on the server-side to push the file -->

Now once the user has reached that page, you can push the data inside that file to the user.

    Layout = null;
    // Get the filename
    var file = Server.MapPath("~/files/" + Request["FileName"]);
    Response.AppendHeader("content-disposition", "attachment; filename=" + Request["img"]);
    Response.ContentType = "application/octet-stream";
    // Now transmit the file

This would transmit the file from the server to the client. No need for any WebService or any other complex framework.

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