2016-07-27 54 views
0

在C#中使用此asp网页上传文件,我需要检查重复项和文件扩展名。检查在c#上传的扩展文件

我接受服务器上传的3个文件。

enter image description here

此代码工作,重复的文件都没有上传和警报弹出是开放的所有重复的文件发送上传。

上传服务器上的前3个文件后,我试图上传另一个.pdf扩展名和一个jpg文件在服务器上。

对于具有.pdf扩展的码的响应的文件是正确的与

'只有.jpg或.JPG或.png扩展名'

第一个困难:

如何为.pdf扩展名添加文件的名称int this alert?

难点之二:

对于现有的响应代码不正确,因为也被惊动了同一台服务器上的另一个JPG文件的存在服务器上的文件JPG。

例如仅上传文件IMG0002A.jpg输出警报弹出我:

  1. “文件IMG0002A.jpg存在” >>>正确
  2. “文件IMG0005A.jpg存在” >>>不正确
  3. '文件IMG0006A.jpg存在' >>>不正确

但我不上传发送的文件:

  • '文件IMG0005A.jpg存在'
  • '文件IMG0006A.jpg存在'
  • 什么问题?

    下面我的代码,谢谢提前。

    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); 
    } 
    

    编辑#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; 
         } 
        } 
    } 
    

    回答

    1

    当文件已经存在,你是通过在objFI和打印消息中的所有文件这是不正确的所有文件循环。

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

    您应该添加一个条件,检查文件名是否与任何的循环,然后打印消息文件只是文件的匹配。

    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; 
        } 
    } 
    
    +0

    感谢您的回复。请参阅我的第一个问题中的**编辑#1 **;我尝试过没有成功。 –

    +0

    请参阅我在上面的回答 –

    +0

    谢谢,但与你的最后建议,编辑,当我尝试上传现有的3个文件警报弹出窗口不会打开文件2和3,只为文件1。 –