/// <summary> /// 上传文件 /// </summary> /// <summary> /// 上传文件 /// </summary> public static UploadModel UploadFile(HttpPostedFileBase file, UNCConfig cfg) { UploadModel model = new UploadModel(); List <string> enabledTypes = new List <string>() { "image/pjpeg", "image/x-png", "image/bmp", "image/png", "image/jpeg" }; //model.FileName = string.Format("{0}.jpg{1}", file.ContentLength, Path.GetExtension(file.FileName)); model.FileName = string.Format("{0}.jpg", file.ContentLength, Path.GetExtension(file.FileName)); model.FileSize = ChatHelper.FormatFileSize(file.ContentLength); model.ReturnFlag = false; if (System.Web.HttpContext.Current.Request.Headers["ocx"] == null && !enabledTypes.Exists(type => type == file.ContentType)) { model.Message = "上传失败:不支持的文件格式。(仅支持.jpg|.png|.bmp格式)"; } else { string fileServer = System.Configuration.ConfigurationManager.AppSettings["fileServer"]; try { using (UNCAccessWithCredentials unc = new UNCAccessWithCredentials()) { if (unc.NetUseWithCredentials(cfg.UNCPath, cfg.User, cfg.Domain, cfg.Password)) { var now = DateTime.Now; var yyyyMM = now.ToString("yyyyMM"); var yyyyMMdd = now.ToString("yyyyMMdd"); var dir = cfg.UNCPath + "\\" + yyyyMM + "\\" + yyyyMMdd + "\\"; var filename = Utils.SHA1Stream(file.InputStream) + ".jpg"; if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } file.SaveAs(dir + filename); model.FileName = Path.GetFileName(file.FileName); model.FileUrl = fileServer + yyyyMM + "/" + yyyyMMdd + "/" + filename; model.ReturnFlag = true; model.Message = "上传成功。"; } } } catch (Exception ex) { model.Message = ex.Message; } } return(model); }
public static UNCConfig GetCfg() { var info = new UNCConfig(); if (string.IsNullOrEmpty(_path)) { _path = System.Web.HttpContext.Current.Server.MapPath("~/lenovochat.io"); } List <string> arr = File.ReadLines(_path, Encoding.UTF8).ToList(); info.UNCPath = arr[0]; info.User = arr[1]; info.Password = arr[2]; if (arr.Count > 3) { info.Domain = arr[3]; } return(info); }