Asp.Net MVC上傳圖片

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章