diff --git a/htdocs/actioncomm.class.php b/htdocs/actioncomm.class.php
index 1b2862b6042d7bafd255073e5153e4e2dd161036..595d80f2134b26169d5ab77ec0db338a609741f3 100644
--- a/htdocs/actioncomm.class.php
+++ b/htdocs/actioncomm.class.php
@@ -555,7 +555,7 @@ class ActionComm
 		$result=create_exdir($conf->agenda->dir_temp);
 		$outputfile=$conf->agenda->dir_temp.'/'.$filename;
 		$result=0;
-		
+
 		$buildfile=true;
 		if ($cachedelay)
 		{
diff --git a/htdocs/comm/action/agendaexport.php b/htdocs/comm/action/agendaexport.php
index c804c23240491a42ef6c251a48f88bc84c01a0d9..14df8b8f245e5add43cc8d5fed18b00ea1e65198 100644
--- a/htdocs/comm/action/agendaexport.php
+++ b/htdocs/comm/action/agendaexport.php
@@ -50,13 +50,14 @@ $mainmenu=isset($_GET["mainmenu"])?$_GET["mainmenu"]:"";
 $leftmenu=isset($_GET["leftmenu"])?$_GET["leftmenu"]:"";
 
 // Define format, type, filename and filter
-$format='vcal';
+$format='ical';
 $type='event';
 $filename='';
 if (! empty($_GET["format"])) $format=$_GET["format"];
 if ($format == 'vcal') $filename='dolibarrcalendar.vcs';
 if ($format == 'ical') $filename='dolibarrcalendar.ics';
 if (! empty($_GET["type"]))   $type=$_GET["type"];
+// Check filename
 if (! $filename)
 {
 	$langs->load("main");
@@ -66,6 +67,8 @@ if (! $filename)
 	llxFooter('$Date$ - $Revision$');
 	exit;
 }
+// Check exportkey
+// \TODO
 $filters=array();
 if (! empty($_GET["year"])) 	$filters['year']=$_GET["year"];
 if (! empty($_GET["idaction"])) $filters['idaction']=$_GET["idaction"];
@@ -76,7 +79,25 @@ $agenda=new ActionComm($db);
 $result=$agenda->build_calfile($format,$type,0,$filename,$filters);
 if ($result >= 0)
 {
-	header("Location: ".DOL_URL_ROOT.'/document.php?modulepart=agenda&file='.urlencode($filename));
+	$attachment = false;
+	$encoding='UTF-8';
+	$type='text/plain';
+	//$type='text/calendar';
+	
+	if ($encoding)   header('Content-Encoding: '.$encoding);
+	if ($type)       header('Content-Type: '.$type);
+	if ($attachment) header('Content-Disposition: attachment; filename="'.$filename.'"');
+	
+	// Ajout directives pour resoudre bug IE
+	//header('Cache-Control: Public, must-revalidate');
+	//header('Pragma: public');
+	 
+	// Clean parameters
+	$outputfile=$conf->agenda->dir_temp.'/'.$filename;
+	$result=readfile($outputfile);
+	if (! $result) print 'File '.$outputfile.' was empty.';
+
+//	header("Location: ".DOL_URL_ROOT.'/document.php?modulepart=agenda&file='.urlencode($filename));
 	exit;
 }
 
diff --git a/htdocs/document.php b/htdocs/document.php
index 19bd70250c3ebdfe1fcb52867dc3420ec5524ebe..8b35a7bb74335b7c40d64523557a5da6461d7d19 100644
--- a/htdocs/document.php
+++ b/htdocs/document.php
@@ -32,7 +32,8 @@ $type = isset($_GET["type"]) ? urldecode($_GET["type"]) : '';
 
 // Define if we need master or master+main
 $needmasteronly=false;
-if ($modulepart == 'webcal') $needmasteronly=true;
+//if ($modulepart == 'webcal') $needmasteronly=true;
+//if ($modulepart == 'agenda') $needmasteronly=true;
 
 // This is to make Dolibarr working with Plesk
 set_include_path($_SERVER['DOCUMENT_ROOT'].'/htdocs');
@@ -40,7 +41,7 @@ set_include_path($_SERVER['DOCUMENT_ROOT'].'/htdocs');
 // Load master or main
 if ($needmasteronly)
 {
-	// Pour companylogo, on charge juste environnement sans logon qui charge le user
+	// For some download we don't need login
 	require("./master.inc.php");
 }
 else
@@ -370,22 +371,6 @@ if ($modulepart)
         $original_file=DOL_DATA_ROOT.'/admin/temp/'.$original_file;
 		$sqlprotectagainstexternals = '';
     }
-
-    // Wrapping for webcalexport
-    if ($modulepart == 'webcal')
-    {
-        $accessallowed=1;
-        $encoding='UTF-8';
-        $original_file=$conf->webcal->dir_temp.'/'.$original_file;
-    }
-
-    // Wrapping for webcalexport
-    if ($modulepart == 'agenda')
-    {
-        $accessallowed=1;
-        $encoding='UTF-8';
-        $original_file=$conf->agenda->dir_temp.'/'.$original_file;
-    }
 }
 
 // Basic protection (against external users only)
diff --git a/htdocs/lib/xcal.lib.php b/htdocs/lib/xcal.lib.php
index 49b6e07cf2eab23a76781de1e09327f8a19b933e..b2117b5e8cc2da65837cad03f3536f6cc918a732 100644
--- a/htdocs/lib/xcal.lib.php
+++ b/htdocs/lib/xcal.lib.php
@@ -193,7 +193,7 @@ function build_calfile($format='vcal',$title,$desc,$events_array,$outputfile,$fi
 		}
 
 		// Footer
-		fwrite($calfileh,"END:VCALENDAR\n");
+		fwrite($calfileh,"END:VCALENDAR");
 		
 		fclose($calfileh);
 	}
diff --git a/htdocs/webcal/webcalexport.php b/htdocs/webcal/webcalexport.php
index 13b3cbf2c35f93158e52d5a5b697b4febb402734..cfbab3a00d58d59e5f74c75fb6de989774272373 100644
--- a/htdocs/webcal/webcalexport.php
+++ b/htdocs/webcal/webcalexport.php
@@ -97,7 +97,25 @@ if (! empty($_GET["year"])) $filters['year']=$_GET["year"];
 $result=$webcal->build_calfile($format,$type,0,$filename,$filters);
 if ($result >= 0)
 {
-	header("Location: ".DOL_URL_ROOT.'/document.php?modulepart=webcal&file='.urlencode($filename));
+	$attachment = false;
+	$encoding='UTF-8';
+	$type='text/plain';
+	//$type='text/calendar';
+	
+	if ($encoding)   header('Content-Encoding: '.$encoding);
+	if ($type)       header('Content-Type: '.$type);
+	if ($attachment) header('Content-Disposition: attachment; filename="'.$filename.'"');
+	
+	// Ajout directives pour resoudre bug IE
+	//header('Cache-Control: Public, must-revalidate');
+	//header('Pragma: public');
+	 
+	// Clean parameters
+	$outputfile=$conf->agenda->dir_temp.'/'.$filename;
+	$result=readfile($outputfile);
+	if (! $result) print 'File '.$outputfile.' was empty.';
+
+//	header("Location: ".DOL_URL_ROOT.'/document.php?modulepart=webcal&file='.urlencode($filename));
 	exit;
 }