2015-09-09 57 views
0

是否有可能返回上傳到ajax的每個文件的路徑,因爲我需要顯示剛剛在前端上傳的圖像。如何從控制器返回更新文件到ajax的文件路徑

public ActionResult Upload() 
     { 

      for (int i = 0; i < Request.Files.Count; i++) 
      { 
       var file = Request.Files[i]; 
       var fileName = Path.GetFileName(file.FileName); 
       var path = Path.Combine(Server.MapPath("~/Images/img"), fileName); 

       file.SaveAs(path); 

      } 


      return Json(new { Success = true, FilePath = "ssssss" }); 
     } 

回答

0
public ActionResult Upload() 
     { 
      //string[] lstfile = new string[]; 

      List<string> lstfile = new List<string>(); 
      for (int i = 0; i < Request.Files.Count; i++) 
      { 
       var file = Request.Files[i]; 
       var fileName = Path.GetFileName(file.FileName); 
       var path = Path.Combine(Server.MapPath("~/Images/img"), fileName); 
       lstfile.Add("/Images/img/" + fileName); 
       file.SaveAs(path); 

      } 


      return Json(new { lstfile }); 
     }