【发布时间】:2011-06-11 04:28:43
【问题描述】:
基本上,我正在尝试从 C# Web 应用程序创建和导出一个 .ics 文件,以便用户可以保存它,并在 Outlook 中打开它以将某些内容添加到他们的日历中。
这是我目前拥有的代码...
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.
再次访问被拒绝错误。
可能是什么问题?
【问题讨论】:
-
顺便说一句,没有“C#.NET”之类的东西。该语言被命名为“C#”。
标签: c# .net asp.net outlook icalendar