3

I'm developing an application to export calendars. The problem that I have is that the calendar is downloaded instead of opened with Outlook. Here is my code:

1) There is an image link in the main page to export the calendar:

<img src="/calendar.png" width="32" Style="width: 32px; height:32px;" title="Calendar" onClick="javascript:document.getElementById('exportCal').submit();">

<form id="exportCal" action="Calendar.jsp" target="_blank" method="post">
    <INPUT TYPE="hidden" ID="calendarData" NAME="calendarData" VALUE=""/>
</form>

2) In the jsp that the link redirects to, I generate the .ics (using the iCal4j library) file and attach it to the response:

response.setHeader ("Content-Disposition", "inline;filename=\"mycalendar.ics\"");
response.setContentType("text/calendar");

Calendar calendar = new Calendar();
calendar.getProperties().add(new ProdId("-//Ben Fortuna//iCal4j 1.0//EN"));
calendar.getProperties().add(Version.VERSION_2_0);
calendar.getProperties().add(CalScale.GREGORIAN);

java.util.Calendar cal = java.util.Calendar.getInstance();
cal.set(java.util.Calendar.MONTH, java.util.Calendar.DECEMBER);
cal.set(java.util.Calendar.DAY_OF_MONTH, 25);

// initialise as an all-day event..
VEvent christmas = new VEvent(new Date(cal.getTime()), "Christmas Day");

// Generate a UID for the event..
UidGenerator ug = new UidGenerator("1");
christmas.getProperties().add(ug.generateUid());

calendar.getComponents().add(christmas);
System.out.println(calendar);

ServletOutputStream fout = response.getOutputStream();

CalendarOutputter outputter = new CalendarOutputter();
outputter.output(calendar, fout);

fout.flush();

And then, the .ics is downloaded. But what I really want to do is open it with Outlook. Is there any way to do that?

Thanks in advance!!

Neets
  • 4,094
  • 10
  • 34
  • 46

1 Answers1

2

This seems to be a browser-specific behavior. The only way to avoid the automatic downloading (in chrome) is by utilizing the webcal protocol as suggested in this SO post. Webcal is typically only used for dynamic calendar subscriptions - not static calendar items. If you open the ICS after download - it should open in Outlook.

<p>A <a href="webcal://mycalendar.ics">webcal ics link</a></p>
Community
  • 1
  • 1
SliverNinja - MSFT
  • 31,051
  • 11
  • 110
  • 173
  • I knew about the webcal protocol but i thought that it was only for static files. Because in order to put a link like that you should have created (and stored) the file previously and then link to it. So every time the page with the link is opened a new calendar file (different for each user) will be generated that may or may not be downloaded. Also, I've tried that solution and I don't know how to save the file in the server so it can be downloaded later. Please correct me if i am wrong and point me to the right direction as I am a newbie with jsp. Thanks a lot!! – Neets Apr 19 '12 at 05:39
  • In current Firefox i get: Prevented navigation to “webcal://www.mydomain/myscript” due to an unknown protocol. The policy about this has appearently changed: https://www.fxsitecompat.dev/en-CA/docs/2020/navigation-to-unknown-protocol-will-be-blocked/ – Fanky May 12 '20 at 10:18