Search code examples
c#asp.nethttpcontext

How to specify the download path while export excel using HttpContext?


I have export table data to Excel. I try this code, but its download to the default Download folder. I would like to download it to the specified path. How to specify the given path?

            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.Buffer = true;
            HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=ConsolidateRpt.xlsx");
            HttpContext.Current.Response.Charset = "";
            HttpContext.Current.Response.ContentType = "application/excel";
            StringWriter writer = new StringWriter();
            HtmlTextWriter writer2 = new HtmlTextWriter(writer);           
            HttpContext.Current.Response.Write(writer.ToString());
            HttpContext.Current.Response.End();

Solution

  • The server can NEVER control where files are downloaded to, on the client. Neither should that ever be possible. The client controls where on the client drive the downloaded file is placed.

    You can only specify the filename, which is fine