开发者

Exporting to Outlook (.ics file) from a .NET web application

开发者 https://www.devze.com 2023-03-09 16:03 出处:网络
Basically I\'m trying to create and export a .ics file from a C# web application so the user can save it, and open it in Outlook to add something to their calendar.

Basically I'm trying to create and export a .ics file from a C# web application so the user can save it, and open it in Outlook to add something to their calendar.

Here's the code I have at the moment...

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(pat开发者_StackOverflow中文版h).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);
}

The above works perfectly. It writes the file and deletes any old ones first.

My main issue is how to get it to the user?

I tried redirecting the page straight to the path of the file:

Response.Redirect(writePath);

It does not work, and throws the following error:

htmlfile: Access is denied.

NOTE: If I copy and paste the contents of writePath, and paste it into Internet Explorer, a save file dialog box opens and allows me to download the .ics file.

I also tried to prompt a save dialog box to download the file,

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();

It does not work either.

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

Access denied error again.

What may be the problem?


It sounds like you are trying to give the user the physical path to the file instead of the virtual path. Try changing the path so it ends up in the www.yoursite.com/date.ics format instead. This will allow your users to download it. The issue is that they don't have access to the C drive on your server.

Here is a link to how to do this:

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

Basically, you need the following line in your code:

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

Use this instead of the Response.Redirect(writePath); and you should be good to go.

0

精彩评论

暂无评论...
验证码 换一张
取 消