title: Using Javascript Ajax call to call MVC action to download object
body: <p>Ive created a simple MVC controller action that creates a simple ics (calendar) item and sends it back through the controller action. As follows:</p>
  
  <pre><code>    public object GenerateICS(int myID)
      {
          iCalendar iCal = new iCalendar();            
           Event evt = iCal.Create&lt;Event&gt;();
          Uri eventLink = new Uri("http://localhost:");
          evt.IsAllDay = false;
  
         evt.Start = new iCalDateTime(DateTime.Now);
         evt.End = new iCalDateTime(DateTime.Now.AddDays(3));
  
         evt.Summary = "MySummary";
         evt.Url = eventLink;
         evt.Description = "You know it";         
  
        Response.ContentType = "text/v-calendar";
        Response.AddHeader("content-disposition", "attachment; filename=" + "Event" + ".ics");
        iCalendarSerializer serializer = new iCalendarSerializer(iCal);
        string result = serializer.SerializeToString(iCal);
        Response.Write(result);          
          return Response;
      }
  </code></pre>
  
  <p>So with the website running, if I go to:</p>
  
  <pre><code>http://localhost:21312/GenerateICS?myID=1 
  </code></pre>
  
  <p>This will generate the ics file server side and pass it back to the client, so your receive a "Do you want to open blah.ics from localhost?". This is perfectly what I want.</p>
  
  <p>My issue is how do I achieve exactly the same by executing it from javascript. I have the following ajax call:</p>
  
  <pre><code> $.ajax({
                  url: "app/GenerateICS",
                  data: { myID: 1 },
                  success: function (data) {
                      //call is successfully completed and we got result in data
                      alert(data);
                  },
                  error: function (xhr, ajaxOptions, thrownError) {
                      //some errror, some show err msg to user and log the error  
                      alert(xhr.responseText);
  
                  }
              });
  </code></pre>
  
  <p>This executes the mvc controller perfectly. But it returns the ics response to the success function. How do I call the controller with ajax but have it so the file is downloaded as I describe how it does when you manually do it?</p>
  
  <p>Thanks</p>