Do not overwrite file downloaded using 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 a file exists, it changes its name, is there any built-in functionality for this? Classic ASP had a parameter, so when you load say house.jpg and then become home again (1) .jpg, etc., which would be useful.

+5
source share
6 answers
var fileName = file.FileName;
var extension = Path.GetExtension(fileName);
var nameWithoutExtension = Path.GetFileNameWithoutExtension(fileName);

var i = 1;
while (File.Exists(uploadFolder + fileName))
{
    fileName = nameWithoutExtension.Trim() + " (" + i + ")" + extension;
    i++;
}

file.SaveAs(uploadFolder + fileName);
+4
source

There is nothing built-in - you will need to create 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
}

It can also be built as a loop:

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);
+7
source

, , File:

bool exists = System.IO.File.Exists(fileName);

(1) , System.IO.Path.GetRandomFileName , . , , .

+2

, , , (1), (2) .. :

public static string GetUniqueFilename(string folder, string postedFileName)
{
    string fileExtension = postedFileName.Substring(postedFileName.LastIndexOf('.') + 1);
    int index = 2;

    while (File.Exists(string.Format("{0}/{1}", folder, postedFileName)))
    {
        if (index == 2)
            postedFileName =
                string.Format("{0} ({1}).{2}",
                              postedFileName.Substring(0, postedFileName.LastIndexOf('.')),
                              index,
                              fileExtension);
        else
            postedFileName =
                string.Format("{0} ({1}).{2}",
                              postedFileName.Substring(0, postedFileName.LastIndexOf(' ')),
                              index,
                              fileExtension);
        index++;
    }

    return postedFileName;
}
+2

GUID, .

.

0

Why don't you delete the file first if the file exists and then the Save method is called?

0
source

All Articles