using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.IO; using System.Linq; using System.Web; using System.Web.Mvc; using YTSoft.BaseCallCenter.Model; using YTSoft.BaseCallCenter.MVCWeb.Models; /// /// 上传文件 /// namespace YTSoft.BaseCallCenter.MVCWeb.Controllers { public class UploadController : BaseController { /// /// 文件上传 /// /// [AcceptVerbs(HttpVerbs.Post)] public ActionResult UploadFile() { string[] filesuffix = ConfigurationManager.AppSettings["filesuffix"].Split(';'); try { if (Request.Files.Count > 0) { string uppath = string.Empty; string savepath = string.Empty; HttpPostedFileBase OrderFile = Request.Files[0]; if (OrderFile != null) { //创建文件新的名称 string nameImg = F_UserCode+DateTime.Now.ToString("yyyyMMddHHmmssfff"); //获得上传文件的路径 string strPath = OrderFile.FileName; //获得上传文件的类型(后缀名) string type = strPath.Substring(strPath.LastIndexOf(".") + 1).ToLower(); if (!filesuffix.Contains(type)) { return Json(new { code = 1, src = "", msg = "不支持格式" }); } int byteCount = OrderFile.ContentLength; if (byteCount > 52428800) { return Json(new { code = 1, src = "", msg = "文件不能大于50M" }); } //拼写数据库保存的相对路径字符串 savepath = "/Content/Upload/orderfile/"; //拼写上传图片的路径 uppath = Server.MapPath("~/Content/Upload/orderfile/"); if (!Directory.Exists(Path.GetDirectoryName(uppath))) { Directory.CreateDirectory(Path.GetDirectoryName(uppath)); } uppath += nameImg + "." + type; savepath += nameImg + "." + type; //上传图片 OrderFile.SaveAs(uppath); //原图片路径 } return Json(new { code = 0, src = savepath, msg = "上传成功" }); } else { return Json(new { code = 1, src = "", msg = "上传出错 请检查文件名称或文件大小" }); } } catch (Exception ex) { return Json(new { code = 1, src = "", msg = "上传出错: " + ex.Message }); } } } }