简体   繁体   English

发布信息时对上载的文件进行整理-ASP.NET C#Web表单

[英]Housekeeping on uploaded files when posting information - asp.net c# web form

I have a webform in asp.net 4.0 c# it's about posting book information to sell. 我在asp.net 4.0 c#中有一个Webform,它是关于发布要出售的图书信息的。

Currently if user upload the book image and not click the "submit" button, the file will still be in my local drive. 当前,如果用户上载图书图片并且未单击“提交”按钮,则该文件仍将位于我的本地驱动器中。 How do I manage those file/housekeeping on unused file that was not any part of post in .net? 如何管理未在.net中发布的未使用文件中的那些文件/内务处理?

Currently it's just using this aspx code: 目前,它仅使用以下aspx代码:

<asp:FileUpload ID="flupload1" runat="server" />
<asp:RegularExpressionValidator ID="revImage" runat="server" ControlToValidate="flupload1"
     ValidationGroup="sell" Display="Dynamic" ForeColor="Red" Text="  Invalid image type"
     ValidationExpression="^.*\.((j|J)(p|P)(e|E)?(g|G)|(g|G)(i|I)(f|F)|(p|P)(n|N)(g|G))$" />

for code behind: 对于后面的代码:

protected void uploadBtn1_Click(object sender, EventArgs e)
{
    if (flupload1.HasFile)
    {
        flupload1.SaveAs(Server.MapPath("productImages") + "//" + flupload1.FileName);
        try
        {
            img1.ImageUrl = "ProductImages//" + flupload1.FileName;
        }
        catch (Exception )
        {

        }
    }
}

The way that we handle similar issues is to have all of the temporary files (uploads, files requested for download) stored in a temporary, well-known directory in the web site. 我们处理类似问题的方法是将所有临时文件(上载,请求下载的文件)存储在网站的一个知名的临时目录中。

Then, when the web app starts (it is scheduled to reset nightly), it removes all files in the directory that are older than 24 hours. 然后,当该网络应用启动时(计划每晚重置一次),它将删除目录中所有早于24小时的文件。

A few ideas for you: 为您提供一些建议:

  1. When the image is first uploaded, cache it in RAM and don't write it to disk until the user subsequently clicks the submit button. 首次上传图像时,请将其缓存在RAM中,直到用户随后单击“提交”按钮时才将其写入磁盘。 Configure the cache entries to expire after a fixed time has elapsed. 将缓存条目配置为在固定时间后过期。
  2. Have a background thread in your ASP.NET application that wakes up every so often and does the required cleanup. 在ASP.NET应用程序中有一个后台线程,该线程每隔一段时间就会唤醒一次并执行所需的清理。 You may be able to simplify this process by saving files to a temp folder first, then moving them to the final destination when the user hits submit. 您可能可以通过以下方式简化此过程:先将文件保存到temp文件夹中,然后在用户点击“提交”时将它们移动到最终目的地。
  3. Create a Scheduled Task in Windows that runs periodically to do the cleanup. 在Windows中创建一个计划任务,该任务定期运行以进行清理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM