码迷,mamicode.com
首页 > Web开发 > 详细

Asp.Net MVC上传图片

时间:2019-03-16 14:13:58      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:div   html   files   code   span   图片   blog   api   www.   

mvc上传图片

 [HttpPost]
        public JsonResult Upload()
        {
            if (Request.Files.Count > 0)
            {
                if (Request.Files.Count == 1)
                {
                    HttpPostedFileBase file = Request.Files[0];
                    if (file.ContentLength > 0)
                    {
                        string title = string.Empty;
                        title = DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + Path.GetFileName(file.FileName);
                        string path = "../UploadFile/" + title;
                        path = System.Web.HttpContext.Current.Server.MapPath(path);
                        file.SaveAs(path);
                        return Json(new { status = true, url = path });
                    }
                }
                else 
                {
                    string[] urllist = new string[Request.Files.Count];
                    for (int i = 0; i < Request.Files.Count; i++)
                    {
                     HttpPostedFileBase file = Request.Files[i];
                     if (file.ContentLength > 0)
                     {
                        string title = string.Empty;
                        title = DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + Path.GetFileName(file.FileName);
                        string path = "../UploadFile/" + title;
                        path = System.Web.HttpContext.Current.Server.MapPath(path);
                        file.SaveAs(path);
                        urllist[i] = path;
                     }
                    }
                    return Json(new { status = true, url = urllist });
                }
              
            }
            else
            {
                return Json(new { status = false, url = "",msg="没有文件" });
            }
         
         
        
            return Json(new { status = false, url = "",msg=""});
        }

详情请看:https://www.cnblogs.com/jingch/p/5036686.html

webapi上传图片请看:https://www.cnblogs.com/webapi/p/10542077.html

Asp.Net MVC上传图片

标签:div   html   files   code   span   图片   blog   api   www.   

原文地址:https://www.cnblogs.com/webapi/p/10542092.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!