Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>For each temporary file that you need to create, make a note of the filename in the session:</p> <pre><code>// create temporary file: string fileName = System.IO.Path.GetTempFileName(); Session[string.Concat("temporaryFile", Guid.NewGuid().ToString("d"))] = fileName; // TODO: write to file </code></pre> <p>Next, add the following cleanup code to global.asax:</p> <pre><code>&lt;%@ Application Language="C#" %&gt; &lt;script RunAt="server"&gt; void Session_End(object sender, EventArgs e) { // Code that runs when a session ends. // Note: The Session_End event is raised only when the sessionstate mode // is set to InProc in the Web.config file. If session mode is set to StateServer // or SQLServer, the event is not raised. // remove files that has been uploaded, but not actively 'saved' or 'canceled' by the user foreach (string key in Session.Keys) { if (key.StartsWith("temporaryFile", StringComparison.OrdinalIgnoreCase)) { try { string fileName = (string)Session[key]; Session[key] = string.Empty; if ((fileName.Length &gt; 0) &amp;&amp; (System.IO.File.Exists(fileName))) { System.IO.File.Delete(fileName); } } catch (Exception) { } } } } &lt;/script&gt; </code></pre> <p><strong>UPDATE</strong>: I'm now accually using a new (improved) method than the one described above. The new one involves HttpRuntime.Cache and checking that the files are older than 8 hours. I'll post it here if anyones interested. Here's my new <strong>global.asax.cs</strong>:</p> <pre><code>using System; using System.Web; using System.Text; using System.IO; using System.Xml; using System.Web.Caching; public partial class global : System.Web.HttpApplication { protected void Application_Start() { RemoveTemporaryFiles(); RemoveTemporaryFilesSchedule(); } public void RemoveTemporaryFiles() { string pathTemp = "d:\\uploads\\"; if ((pathTemp.Length &gt; 0) &amp;&amp; (Directory.Exists(pathTemp))) { foreach (string file in Directory.GetFiles(pathTemp)) { try { FileInfo fi = new FileInfo(file); if (fi.CreationTime &lt; DateTime.Now.AddHours(-8)) { File.Delete(file); } } catch (Exception) { } } } } public void RemoveTemporaryFilesSchedule() { HttpRuntime.Cache.Insert("RemoveTemporaryFiles", string.Empty, null, DateTime.Now.AddHours(1), Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, delegate(string id, object o, CacheItemRemovedReason cirr) { if (id.Equals("RemoveTemporaryFiles", StringComparison.OrdinalIgnoreCase)) { RemoveTemporaryFiles(); RemoveTemporaryFilesSchedule(); } }); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload