在ASP.NET中的会话端删除临时文件 [英] Delete temp files on Session end in ASP.NET

查看:61
本文介绍了在ASP.NET中的会话端删除临时文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站在从pdf转换文档的同时创建了一堆png文件.我已经唯一标识了所有这些文件,并且希望在会话结束时将其删除.我的想法是创建一个列表,将其存储在Session中,然后通过存储在我的列表中的路径删除每个文件.

在Global.asax中,我添加了:

  void Session_End(对象发送者,EventArgs e){如果(Session ["cleanUpCollection"]!= null){List< String>deletePaths =(((List< string>)(Session ["cleanUpCollection"])));;foreach(deletePaths中的String){尝试{System.IO.File.Delete(s);}抓住 { }}}} 

在web.config中,我添加了以下内容:

 < sessionState模式="InProc" cookieless ="false" timeout ="20"/> 

,但文件仍位于临时位置.我还没有测试失败的地方,但是在会话结束时是否有更好的常规做法来删除临时文件?顺便说一句,我已验证并cleanUpCollection存储了本地服务器的路径,因此文件路径中没有错误.

解决方案

一种更可靠的方法是在服务器(站点外部)上运行计划的作业,该作业会定期清理此文件夹,并删除较旧的文件超过一定年龄为此,依靠会话超时是不理想的.

我还要指出,您没有给我们任何指示,指出发生了什么错误或为什么不删除文件.要获得更多帮助,请提供一些信息.

My website creates a bunch of png files while converting documents from pdf. I have all those files uniquely identified and I want them to be removed at session end. My idea was to create a List, store it in Session and delete each file by path stored in my List<>.

In Global.asax I added:

void Session_End(object sender, EventArgs e) 
{
    if (Session["cleanUpCollection"] != null)
    { 
        List<String> deletePaths = ((List<string>)(Session["cleanUpCollection"]));
        foreach(String s in deletePaths)
        {
            try
            {
                System.IO.File.Delete(s);
            }
            catch { }
        }
    }
}

In web.config I added this:

<sessionState mode="InProc" cookieless="false" timeout="20" />

but files still sit in temporary location. I have yet to test where this fails but is there a better, common practice of deleting temporary files at session end? BTW, I verified and cleanUpCollection stores local server's paths so there is no error in file path.

解决方案

A more robust approach to this would be to have a scheduled job running on your server (outside of your site) that periodically cleans up this folder, removing files older than a certain age. Relying on session timeout for this is not ideal.

I would also note that you have given us no indication of what error is occuring or why the files are not deleted. For more help on that, please provide some info.

这篇关于在ASP.NET中的会话端删除临时文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆