IHttpHandler,IHttpModules,以及文件上传后的清理...... [英] IHttpHandler, IHttpModules, and cleaning up after File Uploading...

查看:60
本文介绍了IHttpHandler,IHttpModules,以及文件上传后的清理......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个IHttpHandler,等待上传作为

webmail界面的附件,并将其保存到

config.xml中定义的目录。 br />

我的问题如下:

假设这被假定最终成为其他人使用的组件,

和因此我无法访问他们的global.cs :: Session_End()

如何清理上传的文件 - 但显然是搁浅

当用户中止/放弃写电子邮件?


我的想法是:

*查找目录中创建日期为Now()的文件 - 20

分钟左右。

*但是如何启动它? MyUploader.Dispose ==太早了。也许我可以

启动一个睡眠/计时器 - 但是哪个用户/线程启动它? - 如果每个用户

正在启动一个,这可能会变成一个完全惨败,每个用户

启动辅助线程...

*每次新用户上传时我都可以查找这些旧文件 - 然后我会猜测是否需要清理
。意思是那里总会有文件,永远不会是空的。感觉不对......但是可行......

*我担心这个计时器业务因为 - 一些作家是

慢......他们可能20分钟后完成电子邮件...任何想法?


我调查了一个事实,即Session_Start()是一种方法

IHttpApplication - 但我不喜欢我认为我可以轻而易举地接收到这些信息。

IHttpHandler,或者IHttpModule。

有什么建议吗?


谢谢非常,

天空

解决方案

" Sky Sigal" <关于******* @ xact-solutions.removethis.com>在留言中写道

新闻:%2 **************** @ TK2MSFTNGP10.phx.gbl ...

我有创建了一个IHttpHandler,它等待上传作为
webmail接口的附件,并将其保存到
config.xml中定义的目录。

我的问题如下:
假设这被假定最终作为其他人的组件使用
,因此我无权访问他们的global.cs :: Session_End()
如何清理文件当用户中止/放弃写入电子邮件时,上传了 - 但显然是搁浅了吗?




你确定你的处理程序会被调用吗?电子邮件被放弃了吗?我认为,在整个请求被ASP.NET收到
之前,它不会被调用。这将包括任何和所有上传的文件。

-

John Saunders

johnwsaundersiii at hotmail


确切地说 - 不知道该怎么办...

我现在唯一的想法是每次有人上传时都调用以下

方法新文件...它会清理目录

旧/死文件,只保留新文件。


我的abs。讨厌它是因为它将永远不会回到0文件

本身...

因此我的帖子,寻找其他人如何/已经解决的想法

清理上传目录的问题....

///< summary>

///删除目录中的所有文件超过12小时。

///< / summary>

///< param name =" qDestinationDir">< / param>

///< return> -1如果失败。否则,删除了数量的文件。< / returns>

///< remarks>

///不用说这种方法应该与EXTREME一起使用

护理!

///因为它太危险了,它内置了两个标准:

/// a)给出的目录名必须包含单词''upload''

in,

/// b)如果目录中包含的文件超过7天,然后

它最多

///可能不是当前/工作上传目录,所以没有删除任何东西。

// /这可能看起来很痛苦 - 但是选择删除整个

基础虚拟目录

///意外情况要糟糕得多......

///< / remarks>

public static int CleanDir(string qDestinationDir){

int tResult = -1;

if(qDestinationDir.ToUpper()。IndexOf(" UPLOAD")< -1){

return tResult;

}

System.DateT ime tCheck = System.DateTime.Now.Subtract(new

TimeSpan(-12,0,0));

System.DateTime tCheckWeekAgo = System.DateTime.Now。减去(新的

TimeSpan(-7,0,0,0));

System.DateTime tLastWriteTime =

System.IO.Directory .GetLastWriteTime(qDestinationD ir);

if(tLastWriteTime< tCheckWeekAgo){

//目录保持不动超过一周。

//不能继续。

返回tResult;

}

//时间看:

ArrayList tDeleteMe = new ArrayList();

string [] tFiles = System .IO.Directory.GetFiles(qDestinationDir);

foreach(tFiles中的字符串tFileName){

tLastWriteTime = System.IO.Directory.GetLastWriteTime(tFileName);

if(tLastWriteTime< tCheckWeekAgo){

//文件已经过了一周。

//报警!

//我们不在有效的目录中!!!

返回tResult;

}

if(tLastWriteTime< tCheck) {

//文件已经过了[n]小时。好的最后删除:

tDeleteMe.Add(tFileName);

}

}

tResult = 0 ;

foreach(tDeleteMe中的字符串tFileName){

System.IO.File.Delete(tFileName);

tResult + = 1; < br $>
}

返回tResult;

} //方法:结束


PS:我''我刚刚写了它,所以它编译,但我没有机会

实际运行它...

" John Saunders" <乔************** @ notcoldmail.com>在消息中写道

news:Oz ************** @ TK2MSFTNGP09.phx.gbl ...

" Sky Sigal" <关于******* @ xact-solutions.removethis.com>在消息中写道
新闻:%2 **************** @ TK2MSFTNGP10.phx.gbl ...

我创建了一个IHttpHandler,等待上传为
a webmail界面的附件,并将其保存到
config.xml中定义的目录。

我的问题如下:
假设这被假定最终成为其他人使用的组件,

因此我无法访问他们的global.cs :: Session_End()
如何清理那些文件上传 - 但显然是当用户中止/放弃写电子邮件时搁浅?



如果电子邮件被放弃,你确定你的处理程序会被调用吗? ?



我认为在ASP.NET收到整个请求之前不会调用它。这将包括任何和所有上传的文件。
-
John Saunders
johnwsaundersiii at hotmail



Sky Sigal <关于******* @ xact-solutions.removethis.com>写在消息

新闻:eX ************** @ tk2msftngp13.phx.gbl ...

完全 - 不确定该做什么...


天空,我的观点是,我不相信会有任何死文件

你要清理起来。

-

John Saunders

johnwsaundersiii at hotmail

" John Saunders" <乔************** @ notcoldmail.com>在消息中写道
新闻:Oz ************** @ TK2MSFTNGP09.phx.gbl ...

" Sky Sigal" <关于******* @ xact-solutions.removethis.com>在消息中写道
新闻:%2 **************** @ TK2MSFTNGP10.phx.gbl ...

我创建了一个IHttpHandler,等待上传作为附件
for

a

webmail界面,并将其保存到 config.xml。

我的问题如下:
假设这被假定最终作为其他人使用的组件,

因此我无法访问他们的global.cs :: Session_End()
我如何清理上传的文件 - 但显然是


$ b当用户中止/放弃写入电子邮件时,$ b搁浅?



如果电子邮件是

?我

会认为在ASP.NET收到整个请求之前不会调用它。这将包括任何和所有上传的文件。
-
John Saunders
johnwsaundersiii at hotmail




I have created an IHttpHandler that waits for uploads as attachments for a
webmail interface, and saves it to a directory that is defined in
config.xml.

My question is the following:
assuming that this is suppossed to end up as a component for others to use,
and therefore I do NOT have access to their global.cs::Session_End()
how do I cleanup files that were uploaded -- but obviously left stranded
when the users aborted/gave up writting email?

My thoughts were:
* look for files in the directory that have a create date that is Now() - 20
minutes or so.
* but how do I launch it? MyUploader.Dispose == too early. Maybe I could
launch a sleep/timer -- but which user/thread launches it? -- if every user
is launching one, this could turn into a total fiasco, with each user
launching secondary threads...
* I could look for these ''old files'' every time a new user uploads -- and
clean up then I guess. Meaning there would always be files there, never
empty. Doesn''t feel right...but doable...
* I am worried about this timer business because -- some writers are
SLOW...They might finish email after 20 minutes... any ideas?

I looked into the fact that Session_Start() is a method of
IHttpApplication -- but I don''t think I could wire into that as easily as
IHttpHandler, or IHttpModule.
Any suggestions?

Thank you very much,
Sky


解决方案

"Sky Sigal" <as*******@xact-solutions.removethis.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...

I have created an IHttpHandler that waits for uploads as attachments for a
webmail interface, and saves it to a directory that is defined in
config.xml.

My question is the following:
assuming that this is suppossed to end up as a component for others to use, and therefore I do NOT have access to their global.cs::Session_End()
how do I cleanup files that were uploaded -- but obviously left stranded
when the users aborted/gave up writting email?



Are you sure that your handler will be called if the e-mail is abandonded? I
would think that it wouldn''t be called until the entire request has been
received by ASP.NET. That would include any and all uploaded files.
--
John Saunders
johnwsaundersiii at hotmail


Exactly -- not sure of what to do...
the only idea I have right now is calling the following
method each time someone uploads a new file...It will clean the dir
of old/dead files, leaving the new ones only.

What I abs. hate about it is that it will never get back to 0 files by
itself...
Hence my post, looking for ideas on how others would/have tackled
the problem of cleaning up an upload directory....
/// <summary>
/// Deletes all files in a directory that are older than 12 hours.
/// </summary>
/// <param name="qDestinationDir"></param>
/// <returns>-1 if Failed. Otherwise, num of files deleted.</returns>
/// <remarks>
/// It goes without saying that this method should be used with EXTREME
care!
/// Because it is so dangerous, it comes with two criteria built into it:
/// a) the directory name given must include the word ''upload'' somewhere in
in,
/// b) If the directory has files in it that are older than 7 days, then
it''s most
/// probably NOT a current/working upload directory, so nothing is deleted.
/// This might seem like a pain -- but the option of deleting your whole
base virtual directory
/// by accident is a lot worse...
/// </remarks>
public static int CleanDir(string qDestinationDir){
int tResult =-1;
if (qDestinationDir.ToUpper().IndexOf("UPLOAD")<-1){
return tResult;
}
System.DateTime tCheck = System.DateTime.Now.Subtract(new
TimeSpan(-12,0,0));
System.DateTime tCheckWeekAgo = System.DateTime.Now.Subtract(new
TimeSpan(-7,0,0,0));
System.DateTime tLastWriteTime =
System.IO.Directory.GetLastWriteTime(qDestinationD ir);
if (tLastWriteTime < tCheckWeekAgo){
//Directory remained untouched for over a week.
//Not ok to continue.
return tResult;
}
//Time to look:
ArrayList tDeleteMe = new ArrayList();
string[] tFiles = System.IO.Directory.GetFiles(qDestinationDir);
foreach (string tFileName in tFiles){
tLastWriteTime = System.IO.Directory.GetLastWriteTime(tFileName);
if (tLastWriteTime < tCheckWeekAgo){
//File has been there over a week.
//Alarm!
//We''re not in a valid directory!!!
return tResult;
}
if (tLastWriteTime < tCheck){
//File has been there over [n] hours. Ok to delete at end:
tDeleteMe.Add(tFileName);
}
}
tResult = 0;
foreach (string tFileName in tDeleteMe){
System.IO.File.Delete(tFileName);
tResult +=1;
}
return tResult;
}//Method:End

PS: I''ve just wrote it now, so it compiles, but I havn''t had a chance to
actually run it yet...
"John Saunders" <jo**************@notcoldmail.com> wrote in message
news:Oz**************@TK2MSFTNGP09.phx.gbl...

"Sky Sigal" <as*******@xact-solutions.removethis.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...

I have created an IHttpHandler that waits for uploads as attachments for a webmail interface, and saves it to a directory that is defined in
config.xml.

My question is the following:
assuming that this is suppossed to end up as a component for others to use,

and therefore I do NOT have access to their global.cs::Session_End()
how do I cleanup files that were uploaded -- but obviously left stranded
when the users aborted/gave up writting email?



Are you sure that your handler will be called if the e-mail is abandonded?


I would think that it wouldn''t be called until the entire request has been
received by ASP.NET. That would include any and all uploaded files.
--
John Saunders
johnwsaundersiii at hotmail



"Sky Sigal" <as*******@xact-solutions.removethis.com> wrote in message
news:eX**************@tk2msftngp13.phx.gbl...

Exactly -- not sure of what to do...
Sky, my point is that I don''t believe there will ever be any dead files for
you to clean up.
--
John Saunders
johnwsaundersiii at hotmail
"John Saunders" <jo**************@notcoldmail.com> wrote in message
news:Oz**************@TK2MSFTNGP09.phx.gbl...

"Sky Sigal" <as*******@xact-solutions.removethis.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...

I have created an IHttpHandler that waits for uploads as attachments for

a

webmail interface, and saves it to a directory that is defined in
config.xml.

My question is the following:
assuming that this is suppossed to end up as a component for others to use,

and therefore I do NOT have access to their global.cs::Session_End()
how do I cleanup files that were uploaded -- but obviously left


stranded when the users aborted/gave up writting email?



Are you sure that your handler will be called if the e-mail is


abandonded? I

would think that it wouldn''t be called until the entire request has been
received by ASP.NET. That would include any and all uploaded files.
--
John Saunders
johnwsaundersiii at hotmail




这篇关于IHttpHandler,IHttpModules,以及文件上传后的清理......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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