2014-02-13 46 views
0

林。 我想文件的路徑,存儲到使用EF6 分貝,如果我上傳它只是fiine一個單一的文件,但是當我嘗試使用jQuery的文件上傳到上傳多個文件: 文件被髮送,但不保存到數據庫(被添加到上下文) 但調用SaveChanges僅爲第一個文件。 它有時拋出jQuery的多文件上傳的問題,實體框架6

(sql connection dose not support multiple transactions) 

有的時候它dosnt。 ?!? (怪) 這裏是我的上傳動作:

[HttpPost] 
    public ActionResult Upload(HttpPostedFileBase file) 
    { 
     try 
     { 
      Video video = _repository.Upload<Video>(Request.Files[0]); 
      video.CategoryId = int.Parse(Request.Form["CategoryId"]); 
      int result = _unitOfWork.Commit(); 
      return Json(new 
      { 
       Success = result > 0, 
       Message = "Successfully uploaded..." 
      }); 
     } 
     catch (Exception ex) 
     { 
      return Json(new 
      { 
       Success = false, 
       Message = ex.Message 
      }); 
     } 
    } 

,這裏是我的上傳方式:

public static T Upload<T>(this IRepository<T> repository, HttpPostedFileBase file) 
     where T : DomainEntities.File, IEntity, new() 
    { 
     const string photoPath = "~/Uploads/Photos/"; 
     const string audioPath = "~/Uploads/Audios/"; 
     const string videoPath = "~/Uploads/Videos/"; 

     Type type = typeof(T); 

     T t = new T() 
     { 
      Date = DateTime.Now, 
      Name = file.FileName, 
      Size = file.ContentLength.ToString(), 
      ContentType = file.ContentType 
     }; 

     if (type == typeof(Photo)) 
     { 
      t.Path = Path.Combine(photoPath, file.FileName); 
     } 
     else if (type == typeof(Video)) 
     { 
      t.Path = Path.Combine(videoPath, file.FileName); 
     } 
     else if (type == typeof(Audio)) 
     { 
      t.Path = Path.Combine(audioPath, file.FileName); 
     } 

     file.SaveAs(HttpContext.Current.Server.MapPath(t.Path)); 
     repository.Add(t); 
     //some times its written correctly equal to number of files but some times not. 
     Debug.WriteLine("Added"); 
     return t; 
    } 

更新: 文件被正確saveing到指定的文件系統路徑BTW。

和本所認爲:

<div class="portlet-body form"> 
    <form id="fileupload" class="form-horizontal" action="/Files/Video/Upload" method="POST" enctype="multipart/form-data"> 
     <!-- The fileupload-buttonbar contains buttons to add/delete files and start/cancel the upload --> 
     <div class="row fileupload-buttonbar"> 

      <div class="col-lg-7"> 
       <!-- The fileinput-button span is used to style the file input field as button --> 
       <span class="btn green fileinput-button"> 
        <i class="fa fa-plus"></i> 
        <span> 
         select 
        </span> 
        <input type="file" name="files[]" multiple> 
       </span> 
       <button type="submit" class="btn blue start"> 
        <i class="fa fa-upload"></i> 
        <span> 
         Upload 
        </span> 
       </button> 
       <button type="reset" class="btn yellow cancel"> 
        <i class="fa fa-ban"></i> 
        <span> 
         cancel 
        </span> 
       </button> 
       <input type="checkbox" class="toggle"> 
       <!-- The loading indicator is shown during file processing --> 
       <span class="fileupload-loading"> 
       </span> 
      </div> 
      <!-- The global progress information --> 
      <div class="col-lg-5 fileupload-progress fade"> 
       <!-- The global progress bar --> 
       <div class="progress progress-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100"> 
        <div class="progress-bar progress-bar-success" style="width:0%;"> 
        </div> 
       </div> 
       <!-- The extended global progress information --> 
       <div class="progress-extended"> 
        &nbsp; 
       </div> 
      </div> 
     </div> 
     <!-- The table listing the files available for upload/download --> 
     <table role="presentation" class="table table-striped clearfix"> 
      <tbody class="files"></tbody> 
     </table> 
    </form> 

</div> 

感謝您的幫助。

回答

0

我想,你有2個選擇,我相信,第二個是最好的:

嘗試請求對象

foreach (string file in Request.Files) 
{ 
    //use the Request.Files[file] 
} 

還是工作,你可以重新定義input名稱int途中

<input type="file" name="files" value="" multiple="multiple"/> 

沒有數組定義,你可以嘗試重構你的行動,這樣

public ActionResult Upload(HttpPostedFileBase[] files) 
    foreach (var file in files) { 
     if (file.ContentLength > 0) { 
      // Save your file 
     } 
    } 
} 

它應該工作。

+0

了IEnumerable 爲空時的動作被擊中。我必須直接從Request.Files中讀取。 – SHM

+0

你可以添加用於調用操作的問題的看法的一部分? – Daniele

+0

添加了視圖。 – SHM