2011-06-01 80 views
2

基本上我试图从C#web应用程序创建和导出.ics文件,以便用户可以保存它,并在Outlook中打开它以添加一些内容到他们的日历。从.NET Web应用程序导出到Outlook(.ics文件)

这是我目前所面对的代码...

string icsFile = createICSFile(description, startDate, endDate, summary); 

//Get the paths required for writing the file to a temp destination on 
//the server. In the directory where the application runs from. 

string codeBase = Assembly.GetExecutingAssembly().CodeBase; 
UriBuilder uri = new UriBuilder(codeBase); 
string path = Uri.UnescapeDataString(uri.Path); 
string assPath = Path.GetDirectoryName(path).ToString(); 

string fileName = emplNo + "App.ics"; 
string fullPath = assPath.Substring(0, assPath.Length-4); 

fullPath = fullPath + @"\VTData\Calendar_Event\UserICSFiles"; 

string writePath = fullPath + @"\" + fileName; //writepath is the path to the file itself. 
//If the file already exists, delete it so a new one can be written. 
if (File.Exists(writePath)) 
{ 
    File.Delete(writePath); 
} 
//Write the file. 
using (System.IO.StreamWriter file = new System.IO.StreamWriter(writePath, true)) 
{ 
    file.WriteLine(icsFile); 
} 

上述作品完美。它写入文件并首先删除所有旧文件。

我的主要问题是如何让它给用户?

我试过页面重定向直奔文件的路径:

Response.Redirect(writePath); 

它不工作,并抛出以下错误:

htmlfile: Access is denied. 

注意:如果我复制和粘贴内容writePath,并将其粘贴到Internet Explorer中,保存文件对话框打开并允许我下载.ics文件。

我也试着提示保存对话框,下载文件,

System.Web.HttpResponse response = System.Web.HttpContext.Current.Response; 
response.ClearContent(); 
response.Clear(); 
response.ContentType = "text/plain"; 
response.AddHeader("Content-Disposition", "inline; filename=" + fileName + ";"); 
response.TransmitFile(fullPath); 
response.Flush(); // Error happens here 
response.End(); 

它也不管用。

Access to the path 'C:\VT\VT-WEB MCSC\*some of path omitted *\VTData\Calendar_Event\UserICSFiles' is denied. 

再次访问被拒绝错误。

可能是什么问题?

+0

顺便说一句,没有“C#.NET”这样的东西。该语言被命名为“C#”。 – 2011-06-01 18:33:08

回答

1

听起来好像你正试图给用户物理路径而不是虚拟路径。尝试更改路径,以便以www.yoursite.com/date.ics格式结束。这将允许您的用户下载它。问题是他们无法访问服务器上的C驱动器。

下面是如何做到这一点的链接:

http://www.west-wind.com/weblog/posts/2007/May/21/Downloading-a-File-with-a-Save-As-Dialog-in-ASPNET

基本上,你需要在下面的一行代码:

Response.TransmitFile(Server.MapPath("~/VTData/Calendar_Event/UserICSFiles/App.ics")); 

使用此而不是Response.Redirect(writePath);,你应该很好去。

+0

干杯的响应,试过这个,但我仍然有问题..现在得到错误:** Microsoft JScript运行时错误:Sys.WebForms.PageRequestManagerParserErrorException:从服务器收到的消息无法解析。此错误的常见原因是,通过调用Response.Write(),响应筛选器,HttpModules或服务器跟踪已启用来修改响应时。 Details:错误解析'BEGIN:VCALENDAR VER'附近。** ... – strvanica 2011-06-02 09:23:10

+0

我正在使用'response.ContentType =“text/calendar”;'文本/日历是正确的类型?或将文本/平原更适合?无论哪种方式都产生相同的错误... – strvanica 2011-06-02 09:25:13

+0

@ korvanica - 我相信正确的类型是“application/octet-stream” – IAmTimCorey 2011-06-02 13:46:15

相关问题