2013-05-09 104 views
0

我有下面的C#代碼(這大約是上傳文件到服務器)獲取下一個文件名上傳

for (int i = 0; i < Request.Files.Count-1; i++) 
       { 
        if (Request.ContentLength != 0) 
        { 
         int Size = Request.Files[i].ContentLength/1024; 
         if (Size <= 512) 
         { 
          string LocalFile = Request.Files[i].FileName; 
          int dot = LocalFile.LastIndexOf('.'); 
          string FileType = LocalFile.Substring(dot + 1); 
          if (FileType == "gif" || FileType == "jpg" || FileType == "png" || FileType == "GIF" || FileType == "JPG" || FileType == "PNG") 
          { 
           int LastIndex = LocalFile.LastIndexOf(@"\") + 1; 
           string File = LocalFile.Substring(LastIndex, LocalFile.Length - LastIndex); 
           string Path = Server.MapPath(" ../images/tracks") + "..\\" + File; 
           Request.Files[i].SaveAs(Path); 
           if (i != Request.Files.Count - 1) 
            ImageList += string.Format("images/tracks/{0}|", File); 
           else { ImageList += string.Format("images/tracks/{0}", File); } 
          } 
         } 
         else 
         { 
          Response.Write("The file is too big !"); 
         } 
        } 
        else 
        { 
         Response.Write("Unknown Error !"); 
        } 
       } 

的問題是,有多個文件上傳字段。

我要創造條件,將檢查是否有文件後,文件的[I](麗澤如果文件[I + 1]空) 如果是程序將執行此代碼:ImageList += string.Format("images/tracks/{0}", File);

其他: ImageList += string.Format("images/tracks/{0}|", File);

我的問題是這樣的情況應該是怎樣的?

尋求幫助,謝謝!

+0

你不應該需要經過的一個進行檢查,因爲你已經通過文件的全長循環'的for(int i = 0; I <請求。 Files.Count-1; i ++)' - 或者我誤解了這個問題? – Darren 2013-05-09 20:33:00

回答

1

只要刪除您的條件(如果)並添加結尾字符的所有字符串。在循環結束時,如果它是「|」,則可以刪除最後一個字符。

您的代碼:

for (int i = 0; i < Request.Files.Count-1; i++) 
      { 
       if (Request.ContentLength != 0) 
       { 
        int Size = Request.Files[i].ContentLength/1024; 
        if (Size <= 512) 
        { 
         string LocalFile = Request.Files[i].FileName; 
         int dot = LocalFile.LastIndexOf('.'); 
         string FileType = LocalFile.Substring(dot + 1); 
         if (FileType == "gif" || FileType == "jpg" || FileType == "png" || FileType == "GIF" || FileType == "JPG" || FileType == "PNG") 
         { 
          int LastIndex = LocalFile.LastIndexOf(@"\") + 1; 
          string File = LocalFile.Substring(LastIndex, LocalFile.Length - LastIndex); 
          string Path = Server.MapPath(" ../images/tracks") + "..\\" + File; 
          Request.Files[i].SaveAs(Path); 
          //if (i != Request.Files.Count - 1) 
           ImageList += string.Format("images/tracks/{0}|", File); 
          //else { ImageList += string.Format("images/tracks/{0}", File); } 
         } 
        } 
        else 
        { 
         Response.Write("The file is too big !"); 
        } 
       } 
       else 
       { 
        Response.Write("Unknown Error !"); 
       } 
      } 
      //Remove the last character 
      if (ImageList.EndsWith("|")) ImageList = ImageList.Remove(ImageList.Length - 1, 1); 
} 
+0

非常感謝!那是我尋找的! – 2013-05-10 06:12:47