t_onny
t_onny

Reputation: 1

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.

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?

Currently it's just using this aspx code:

<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 )
        {

        }
    }
}

Upvotes: 0

Views: 395

Answers (2)

RickNZ
RickNZ

Reputation: 18654

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. 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. 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.
  3. Create a Scheduled Task in Windows that runs periodically to do the cleanup.

Upvotes: 0

competent_tech
competent_tech

Reputation: 44931

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.

Upvotes: 1

Related Questions