Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Don't overwrite file uploaded through FileUpload control

With the following code:

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            string fileExt =
               System.IO.Path.GetExtension(FileUpload1.FileName);

            if (fileExt == ".jpg" || fileExt == ".jpeg" || fileExt == ".gif" || fileExt == ".png")
            {
                try
                {
                    FileUpload1.SaveAs(Server.MapPath("../uploads/originals/" + FileUpload1.FileName));
                    Label1.Text = "File name: " +
                        FileUpload1.PostedFile.FileName + "<br>" +
                        FileUpload1.PostedFile.ContentLength + " kb<br>" +
                        "Content type: " +
                        FileUpload1.PostedFile.ContentType;
                }
                catch (Exception ex)
                {
                    Label1.Text = "ERROR: " + ex.Message.ToString();
                }
            }
            else
            {
                Label1.Text = "Only image files are allowed!";
            }
        }
        else
        {
            Label1.Text = "You have not specified a file.";
        }


    }

I want to make it so that if the file exists it changes the name of it, is there any built in functionality for this? Classic ASP had a parameter so that when you upload say house.jpg, and then again it would become house(1).jpg etc etc which was useful.

like image 335
Tom Gullen Avatar asked Sep 02 '10 08:09

Tom Gullen


People also ask

How do I stop FileUpload control from clearing on postback?

The simple solution for preventing file loss from an upload control on postback is to put the upload control outside of an update panel control on a . aspx page. Or, in other words, put all the input controls that might trigger a postback inside an update panel. eg.

What is FileUpload control in asp net?

ASP. NET's FileUpload is an input controller used to upload files to a server. It appears on the screen with a browse button and opens up a dialogue box to choose a file or multiple files to upload from the local storage to the server. This is a server-side control provided by ASP.NET.

What is FileUpload?

Uploading is the transmission of a file from one computer system to another, usually larger computer system. From a network user's point-of-view, to upload a file is to send it to another computer that is set up to receive it.


1 Answers

There is nothing built in - you will need to make your own algorithm:

string path = Server.MapPath("../uploads/originals/" + FileUpload1.FileName);

if(!File.Exists(path))
{
  FileUpload1.SaveAs(path);
}
else
{
  // figure a different file name, perhaps check for existence as well
}

This can be constructed as a loop as well:

string path = Server.MapPath("../uploads/originals/" + FileUpload1.FileName);

while(File.Exists(path))
{
  // GetAlternatePath generates a new filename based on the path passed in
  path = GetAlternatePath(path); 
}
FileUpload1.SaveAs(path);
like image 78
Oded Avatar answered Sep 23 '22 10:09

Oded