1

Ive created a simple MVC controller action that creates a simple ics (calendar) item and sends it back through the controller action. As follows:

    public object GenerateICS(int myID)
    {
        iCalendar iCal = new iCalendar();            
         Event evt = iCal.Create<Event>();
        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;
    }

So with the website running, if I go to:

http://localhost:21312/GenerateICS?myID=1 

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.

My issue is how do I achieve exactly the same by executing it from javascript. I have the following ajax call:

 $.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);

                }
            });

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?

Thanks

Matt
  • 3,125
  • 11
  • 39
  • 90
  • why are you trying to do file download through AJAX? – charlietfl Oct 31 '12 at 01:50
  • I dont want to download file through ajax. I just don't currently know of how to execute the MVC action from javascript in another way that will return the downloaded file? – Matt Oct 31 '12 at 01:51
  • 2
    Similar to this [SO](http://stackoverflow.com/questions/3597179/file-download-in-asp-net-mvc-2) See the accepted answer – heads5150 Oct 31 '12 at 01:52

3 Answers3

1

Thanks to @heads5150 for the link.

It was just the fact of setting the browser location with:

document.location.href = "app/GenerateICS?...";
Alberto Zaccagni
  • 28,473
  • 10
  • 71
  • 102
Matt
  • 3,125
  • 11
  • 39
  • 90
  • App/generateICS creates the file, on complete window.location.href should be given the url of the completed ics file, may wanna get a little dynamic as far as naming is concerned – Jay Rizzi Oct 31 '12 at 02:53
0

In your ajax success , put this

  Window.location.href='yourICSfileLink';

This would redirect the browser on success of the ajax creating the file to then open or in this case download the ics file, note this would happen each time ajax success occured

Jay Rizzi
  • 3,922
  • 5
  • 33
  • 62
0

I was just looking for downloading from js and the answer is generally related to creating an iframe and doing through it. There are even some jquery plugins that do so. Some eg:

Download File Using Javascript/jQuery

http://johnculviner.com/category/jQuery-File-Download.aspx

If you google it you can find even more about the topic.

Community
  • 1
  • 1
Robyflc
  • 1,209
  • 9
  • 16