prabhu p

prabhu p

  • 2k
  • 181
  • 98.1k

How to download a file from handler in asp.net

Nov 24 2016 6:23 AM
In web method 
 
I am getting data in datatable and save in local path as excel file from there i want call handler and file want to download, while debugging  everything is fine but file not downloading
 
[System.Web.Services.WebMethod]
public static void SaveME_Onlinedetails(string[] CartValues)
{
 
Objdt = OBJBLL.BLLLOADGRID(objboNova);
string strname = "ManualEntry";
ExcelFile ef = new ExcelFile();
ExcelWorksheet ws = ef.Worksheets.Add("Summary");
string filepath = "E:\\BackUp\\Hemachandraprabhu_18112016\\Novartis\\Novartis\\ManualEntry\\ManualEntry " + strname + ".XLS";
FileStream fs = File.Create(filepath);
ws.InsertDataTable(Objdt, "A1", true);
ef.SaveXls(fs);
 WebClient client = new WebClient();
client.DownloadFile("http://localhost:58294/Upload.ashx", "ManualEntry"); 
 
handler upload.ashx
 
[System.Web.Services.WebMethod(EnableSession = true)]
public void ProcessRequest(HttpContext context)
{
try
{
HttpPostedFile postedFile = context.Request.Files["Filedata"];
string fileName = "ManualEntry.XLS";
string savepath = "";
string tempPath = "";
tempPath = System.Configuration.ConfigurationManager.AppSettings["ManualExcel"];
savepath = context.Server.MapPath(tempPath);
context.Response.ContentType = "application/ms-excel";
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + "ManualEntry");
context.Response.WriteFile(savepath + fileName);
HttpContext.Current.ApplicationInstance.CompleteRequest();
 
}
catch (Exception ex)
{
throw ex;
}
}
 
 
Please help me 
 
 

Answers (4)