2014-05-12 165 views
0

我已經開發了一種方法來從表單接收文件並在保存文件後從每個文件返回信息。也許在互聯網上有一個類似的,但我沒有找到,但是我仍然在學習c#,並且我喜歡建立一些方法來幫助我。使用ASP.NET MVC上傳多個文件

我想用2個建議發佈,其中一個是檢查我是否確定,另一個原因是共享代碼。

有什麼建議嗎?

如果你喜歡它!使用它:)

我沒有時間評論整個代碼,對不起! :(

方法

 public String[,] upload(IEnumerable<HttpPostedFileBase> _files, List<string> _flSupportedTypes,int _flSizeLimit, string _serverPath) 
     { 
      /* 
      * Array Details 
      * 
      * { Fields detail } 
      * [1~] [0] - Status code 
      *  [1] - Status message 
      *  [2] - Upload file Name 
      *  [3] - New file name 
      *  [4] - Virtual Path 
      *  [5] - Local Path 
      *  
      * { General details } 
      * [0] [0] - Status Code 
      *  [1] - Status message 
      *  [2] - total fields 
      *  [3] - total fields processed 
      */ 
      int totalFieldsUpload = _files != null ? _files.Count() : 0; 
      int countFieldsUpload = 0; 
      int countFilesUpload = 0; 
      String[,] filesResult = new String[totalFieldsUpload + 1, 6]; 

      if(totalFieldsUpload == 0) 
      { 
       filesResult[0, 0] = "0"; 
       filesResult[0, 1] = "No fields"; 
       filesResult[0, 2] = "0"; 
       filesResult[0, 3] = "0"; 
      } 
      else 
      { 
       filesResult[0, 0] = "1"; 
       filesResult[0, 1] = "OK"; 
       filesResult[0, 2] = totalFieldsUpload.ToString(); 

       if (!Directory.Exists(_serverPath)) 
        Directory.CreateDirectory(_serverPath); 

       foreach (var file in _files) 
       { 
        bool isContentOK = false; 
        countFieldsUpload++; 

        if (file != null) 
        { 
         String newfileName = DateTime.Now.ToFileTimeUtc() + 
               "_" + Path.GetRandomFileName() + 
               "." + file.FileName.ToString().Split('.').Last(); 

         String localPath = _serverPath + newfileName; 
         String virtualPath = localPath.Replace(Request.ServerVariables["APPL_PHYSICAL_PATH"], "~/").Replace(@"\", "/"); 

         if (file.ContentLength <= _flSizeLimit) 
         { 
          foreach (var type in _flSupportedTypes) 
          { 
           if (file.ContentType == type) 
           { 
            file.SaveAs(localPath); 
            isContentOK = true; 

            countFilesUpload++; 

            break; 
           } 
           else 
            isContentOK = false; 

          } 

          filesResult[countFieldsUpload, 0] = "1"; 
          filesResult[countFieldsUpload, 1] = isContentOK ? "OK" : "ContentType Failed"; 
          filesResult[countFieldsUpload, 2] = file.FileName; 
          filesResult[countFieldsUpload, 3] = newfileName; 
          filesResult[countFieldsUpload, 4] = virtualPath; 
          filesResult[countFieldsUpload, 5] = localPath; 
         } 
         else 
         { 
          filesResult[countFieldsUpload, 0] = "0"; 
          filesResult[countFieldsUpload, 1] = "Size Failed"; 
          filesResult[countFieldsUpload, 2] = file.FileName; 
         } 

        } 
        else 
        { 
         filesResult[countFieldsUpload, 0] = "0"; 
         filesResult[countFieldsUpload, 1] = "Field empty"; 
        } 
       } 

       filesResult[0, 3] = countFilesUpload.ToString(); 
      } 

      return filesResult;    
     } 

HTML

@using (Html.BeginForm("ActionTest", "Controller", FormMethod.Post, new { enctype = "multipart/form-data" })) 
{ 
    @Html.AntiForgeryToken() 
    Html.EnableClientValidation(false); 
    <div class="form-group"> 
     <label for="photo">Photo:</label> 
     <input type="file" name="photo[0]" id="photo_0"> 
     <input type="file" name="photo[1]" id="photo_1"> 
     <input type="file" name="photo[2]" id="photo_2"> 
     <input type="submit" /> 
    </div> 
} 

作用的結果

[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult ActionTest(IEnumerable<HttpPostedFileBase> photo) 
{ 
    List<string> supportedTypes = new List<string>() { "image/jpeg", "image/gif", "image/png" }; 
    String   serverPath  = Server.MapPath("/") + ConfigurationManager.AppSettings["imgTmpUploadPath"].ToString(); 
    String[,]  filesResult  = upload(photo, supportedTypes, 1048576, serverPath); 

    return View(); 
} 

回答

1

您可以從閱讀這樣的對象:

[HttpPost] 
    public ActionResult Page2(FormCollection objCollection) 
    { 
     foreach (string fileName in Request.Files) 
     { 

     HttpPostedFileBase file = Request.Files[fileName]; 

     ... 
     } 
    } 
+0

謝謝! :)我會努力的。 – Canela