清除asp.net中的临时上传文件文件夹
我想知道以下场景的最佳实践是什么:
用户在页面 A 上上传文件。该文件存储在唯一的临时文件夹中(基于用户 GUID)。用户被重定向到页面 B,其中执行文件验证并最终重定向到页面 C。
所有这些页面都使用用户上传的文件。
显然,在某些时候我想删除这些文件以防止服务器塞满临时文件。我想到了几个解决方案,但我想知道是否有更好的解决方案/其中哪一个是最好的。
- :
解决方案 1:进程完成后删除文件(在此案例,第 c) 页。我认为这不是一个好的解决方案,因为用户很可能在中途某个地方取消了他的操作(页面 B)。这仍然会导致临时文件保留在服务器上
解决方案 2:加载页面 A 时,检查主临时文件夹,然后删除所有文件和临时文件。 。
解决方案 3(这可能是最干净的):编写一个小型控制台应用程序,其功能与解决方案 2 相同,并使用 Windows 任务计划程序安排它运行一次 解决方案
解决方案 4 : 与 3 相同,但是在 Windows 服务中。我认为为此创建一个 Windows 服务有点矫枉过正。
现在,我很想说 3 是最干净的,但由于它对于我所拥有的应用程序来说似乎有点过大,我认为解决方案 1 和 3 的组合。 2 做出一个好的替代方案。
有什么建议/反馈吗?
谢谢!
I was wondering what the best practice was for the following scenario:
A user uploads a file on page A. the file is stored in a unique temp folder (based on user GUID). The user is redirected to page B where validation of the file is performed and eventually redirected to page C.
All of these pages use the file(s) uploaded by the user.
Obviously, at some point I want to remove those files to prevent the server from getting stuffed with temp files. I've got a couple of solutions that come to mind, but I was wondering if there is a better solution/which one of those is the best.:
Solution 1: Delete the file after the process is completed (in this case, on Page c). I don't think this is a good solution, because it's well possible that the user cancels his action somewhere halfway (page B). This would still cause temp files to stay on the server
Solution 2: On onload of page A, do a check for in the main temp folder, and delete all files & folders which haven't been used in the last x days
Solution 3 (which is probably the cleanest): write a small console app that will do the same as solution 2, and schedule it with windows task scheduler to run once a day at night
Solution 4: Same as 3, but in a windows service. I'd think it's a bit overkill to create a windows service for that.
Now, I'm tempted to say 3 is the cleanest, but since it seems a bit overkill for the app I'm having, I think the combination of solution 1 & 2 make a good alternative.
Any suggestions / feedback?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想说,想法 1 和 3 的结合非常棒。允许页面在作业完成后删除文件(随时清除可以删除的内容,以免浪费存储空间)。然后,设置一个“备份”计划任务,在网站最不活跃的时间每天运行一次,并删除早于您确定的阈值的任何备用文件。
您不需要每天删除一次文件的长时间运行进程的开销(解决方案 4)。
解决方案 2 对我来说尤其“糟糕”,因为它降低了网站的感知性能 - 删除文件需要时间,尤其是当(HDD)磁盘受到破坏时。
I'd say that a combination of ideas 1 and 3 are great. Allow the page to delete the file after a job has been completed (clear what you can as you go so you don't waste storage space). Then, have a "backup" scheduled task that runs once a day during the least active time of your website, and delete any spare files that are older than a threshold that you determine.
You don't need the overhead of a long-running process that deletes files once a day (solution 4).
Solution 2 particularly stands out to me as bad because it reduces the perceived performance website - deleting a file takes time, especially when the (HDD) disk is being thrashed.