如何从控制器返回更新文件到ajax的文件路径

时间:2015-09-09 07:39:58

标签: json ajax asp.net-mvc

是否可以返回上传到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" });
        }

1 个答案:

答案 0 :(得分: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 });
        }