【问题标题】:Exporting to Outlook (.ics file) from a .NET web application从 .NET Web 应用程序导出到 Outlook(.ics 文件)
【发布时间】: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


【解决方案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);,你应该很高兴。

【讨论】:

  • 为响应干杯,尝试了但仍然有问题..现在收到错误:Microsoft JScript 运行时错误:Sys.WebForms.PageRequestManagerParserErrorException:无法从服务器收到的消息解析。此错误的常见原因是通过调用 Response.Write()、响应过滤器、HttpModules 或启用了服务器跟踪来修改响应。详细信息:在“BEGIN:VCALENDAR VER”附近解析出错。 ...
  • 我正在使用 'response.ContentType = "text/calendar";'文本/日历是正确的类型吗?还是 text/plain 更适合?无论哪种方式都产生相同的错误...
  • @korvanica - 我相信正确的类型是“application/octet-stream”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-06
相关资源
最近更新 更多