Click here to Skip to main content
15,905,785 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I am implementing file downloading feature. every thing works fine but instead of downloading file is opening in browser. below is my code. i created a custom ActionResult and called it from controller. this Action Method is called from a anchor click event.

This is my ActionMethod

C#
public ActionResult DownLoadSubmission(int NodeNo)
      {
          if (NodeNo > 0)
          {
              DataSet ds = DAL.GetDataSet("   select * from Wroks Where NodeNo="+ NodeNo);
              if (ds != null && ds.Tables.Count > 0 && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
              {
                 // DownLoadResult obj = new DownLoadResult(ds.Tables[0].Rows[0]["ActualFileName"].ToString(), ds.Tables[0].Rows[0]["ServerFileName"].ToString());

                  return new  DownLoadResult{FileDownLoadName= ds.Tables[0].Rows[0]["ActualFileName"].ToString(),FileServerName= ds.Tables[0].Rows[0]["ServerFileName"].ToString()};
              }
          }
          return new EmptyResult();
          //return Content("");
      }


This is custom ActionResult
C#
public class DownLoadResult : ActionResult
{

    public string FileDownLoadName { get; set; }
    public string FileServerName { get; set; }
    public DownLoadResult()
    {

    }
    public DownLoadResult(string orgName, string serverName)
    {
        this.FileDownLoadName = orgName;
        this.FileServerName = serverName;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        string Path = "", PhysicalPath = "";

        Path = context.HttpContext.Request.ApplicationPath;
        PhysicalPath = context.HttpContext.Request.MapPath(Path);
        string strCompletePath = "";
        strCompletePath = PhysicalPath + "Submissions\\" + FileServerName;
        context.HttpContext.Response.AddHeader("content=disposition", "attachment;filename=" + FileDownLoadName);
        context.HttpContext.Response.TransmitFile(strCompletePath);
    }
}


I am calling Controller ActionMethod from through following anchor click.
JavaScript
$("#tblWorkList").on("click", "a.download", function (e) {
    debugger
    e.preventDefault();
    var url="@Url.Action("DownLoadSubmission", "Home", new { NodeNo = "-11" })";
    url=url.replace("-11",$(this).attr("data-submissionid"))
    window.location.href = url;

});


I am unable to download file (file is opening in browser), Please help me..
Posted

1 solution

The header should be Content-Disposition not content=disposition
 
Share this answer
 

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