title: File download in Asp.Net MVC 2
body: <p>I want to enable file download in my MVC application, without simply using a hyperlink. I plan to use an image or the like and make it clickable by using jQuery. At the moment I have a simple  just for testing.</p>
  
  <p>I found an explanation of doing the download through an action method, but unfortunately the example still had actionlinks.</p>
  
  <p>Now, I can call the download action method just fine, but nothing happens. I guess I have to do something with the return value, but I don't know what or how.</p>
  
  <p>Here's the action method:</p>
  
  <pre><code>    public ActionResult Download(string fileName)
      {
          string fullName = Path.Combine(GetBaseDir(), fileName);
          if (!System.IO.File.Exists(fullName))
          {
              throw new ArgumentException("Invalid file name or file does not exist!");
          }
  
          return new BinaryContentResult
          {
              FileName = fileName,
              ContentType = "application/octet-stream",
              Content = System.IO.File.ReadAllBytes(fullName)
          };
      }
  </code></pre>
  
  <p>Here's the BinaryContentResult class:</p>
  
  <pre><code>public class BinaryContentResult : ActionResult
  {
      public BinaryContentResult()
      { }
  
      public string ContentType { get; set; }
      public string FileName { get; set; }
      public byte[] Content { get; set; }
  
      public override void ExecuteResult(ControllerContext context)
      {
  
          context.HttpContext.Response.ClearContent();
          context.HttpContext.Response.ContentType = ContentType;
  
          context.HttpContext.Response.AddHeader("content-disposition",
  
                                                 "attachment; filename=" + FileName);
  
          context.HttpContext.Response.BinaryWrite(Content);
          context.HttpContext.Response.End();
      }
  }
  </code></pre>
  
  <p>I call the action method via:</p>
  
  <pre><code>&lt;span id="downloadLink"&gt;Download&lt;/span&gt;
  </code></pre>
  
  <p>which is made clickable via:</p>
  
  <pre><code>$("#downloadLink").click(function () {
      file = $(".jstree-clicked").attr("rel") + "\\" + $('.selectedRow .file').html();
      alert(file);
      $.get('/Customers/Download/', { fileName: file }, function (data) {
          //Do I need to do something here? Or where?
      });
  });
  </code></pre>
  
  <p>Note that the fileName parameter is received correctly by the action method and everything, it's just that nothing happens so I guess I need to handle the return value somehow?</p>