Antonio Mailtraq
Antonio Mailtraq

Reputation: 1407

Check for extention files on upload in c#

With this asp net page upload file in C# I need to check for duplicates and file extensions.

I accept 3 files in upload on the server.

enter image description here

This code worked and the duplicates file are not uploaded and the alert popup is open for all duplicate files send to upload.

After uploading the first 3 files on the server, I have tried to upload another file with .pdf extension and one jpg existing on the server.

For the file with .pdf extension the response of code is correct with

'Only .jpg or .JPG or .png extension'

First difficulty :

How to do for add the name of file with .pdf extension int this alert ?

Second difficulty:

For the file jpg existing on the server the response code is incorrect because is also alerted of presence of another jpg file on the same server.

e.g. upload only the file IMG0002A.jpg in output alert popup I have :

  1. 'The file IMG0002A.jpg exists' >>> correct
  2. 'The file IMG0005A.jpg exists' >>> not correct
  3. 'The file IMG0006A.jpg exists' >>> not correct

But I don't send in upload the files :

  1. 'The file IMG0005A.jpg exists'
  2. 'The file IMG0006A.jpg exists'

What's the problem ?

My code below, thank you in advance.

if (FileExstention == ".jpg" || FileExstention == ".JPG" || FileExstention == ".png")
{
    if (File.Exists(theFileName))
    {
        objDir = new DirectoryInfo(Server.MapPath("\\images\\);
        objFI = objDir.GetFiles("*.*");
        iFileCnt = 0;

        if (objFI.Length > 0)
        {
            foreach (FileInfo file in objFI)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "Msg" + iFileCnt, "alert('The file " + file.Name + " exists');", true);
                iFileCnt += 1;
            }
        }
    }
    else
    {
        if (!Directory.Exists(directoryPath))
        {
            Directory.CreateDirectory(directoryPath);
            upload.SaveAs(directoryPath + "\\" + Path.GetFileName(theFileName));
        }
        else
        {
            upload.SaveAs(directoryPath + "\\" + Path.GetFileName(theFileName));
        }

        objDir = new DirectoryInfo(Server.MapPath("\\images\\);
        objFI = objDir.GetFiles("*.*");
        iFileCnt = 0;

        if (objFI.Length > 0)
        {
            foreach (FileInfo file in objFI)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "Msg" + iFileCnt, "alert('Saved file " + file.Name + ".');", true);
                iFileCnt += 1;
            }
        }
    }
}
else
{
    Page.ClientScript.RegisterStartupScript(this.GetType(), "Msg", "alert('Only .jpg or .JPG or .png exstention');", true);
}

Edit #1

if (objFI.Length > 0)
{
    foreach (FileInfo file in objFI)
    {
        bool exists = file.Length > 0;

        if (exists)
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "Msg" + iFileCnt, "alert('The file " + file.Name + " exists');", true);
            iFileCnt += 1;
        }
    }
}

Upvotes: 0

Views: 235

Answers (1)

Microsoft DN
Microsoft DN

Reputation: 10020

When file already exists, you are looping through all the files in objFI and printing message for all files which is incorrect.

foreach (FileInfo file in objFI)
{
       Page.ClientScript.RegisterStartupScript(this.GetType(), "Msg" + iFileCnt, "alert('The file " + file.Name + " exists');", true);
                iFileCnt += 1;
}

You should add a condition to check whether the file name matches with any of the files in loop and then print message for that file only.

foreach (FileInfo file in objFI)
{
    if(file.Name == theFileName)
    {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "Msg" + iFileCnt, "alert('The file " + file.Name + " exists');", true);
                iFileCnt += 1;
    }
}

Upvotes: 1

Related Questions