public async Task <PicResultData> RegistPicture([FromServices] IHostingEnvironment environment) { var data = new PicResultData(); string path = string.Empty; var files = Request.Form.Files; string strpath = ""; string strpath1 = ""; if (files == null || files.Count() <= 0) { data.Msg = "请选择上传的文件。"; data.Code = "404"; return(data); } try { if (files.Sum(c => c.Length) <= 1024 * 1024 * 2) { foreach (var file in files) { string[] sArray = file.FileName.Split("."); //strpath1 = DateTime.Now.ToString("yyyyMMddHHmmss") + Guid.NewGuid().ToString().Replace("-", "") + // file.FileName.Substring(file.FileName.LastIndexOf("."), (file.FileName.Length - file.FileName.LastIndexOf("."))); strpath1 = DateTime.Now.ToString("yyyyMMddHHmmss") + sArray[0] + file.FileName.Substring(file.FileName.LastIndexOf("."), (file.FileName.Length - file.FileName.LastIndexOf("."))); //strpath1 = files[0].FileName; strpath = Path.Combine("UploadFiles/PersonalDiary", strpath1); path = Path.Combine(environment.WebRootPath, strpath); using (var stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite)) { await file.CopyToAsync(stream); } } var response = ResponseModelFactory.CreateInstance; data.Code = "200"; data.Path = strpath1; data.Msg = "上传成功"; return(data); } else { data.Code = "404"; data.Msg = "文件过大"; return(data); } } catch (Exception) { data.Code = "404"; data.Msg = "文件上传失败"; return(data); } }
public async Task <PicResultData> RegistPicture([FromServices] IHostingEnvironment environment) { var data = new PicResultData(); string path = string.Empty; var files = Request.Form.Files; string strpath = ""; string strimage = ""; if (files == null || files.Count() <= 0) { data.Msg = "请选择上传的文件。"; data.Code = "404"; return(data); } //格式限制 var allowType = new string[] { "image/jpg", "image/png", "image/jpeg", "image/gif", "image/bmp" }; if (files.Any(c => allowType.Contains(c.ContentType))) { if (files.Sum(c => c.Length) <= 1024 * 1024 * 2) { foreach (var file in files) { strpath = Path.Combine("UploadFiles/RegistPicture", DateTime.Now.ToString("MMddHHmmss") + Guid.NewGuid().ToString().Replace("-", "") + file.FileName.Substring(file.FileName.LastIndexOf("."), (file.FileName.Length - file.FileName.LastIndexOf(".")))); path = Path.Combine(environment.WebRootPath, strpath); if (strimage == "") { strimage = strpath; } else { strimage += "|" + strpath; } using (var stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite)) { await file.CopyToAsync(stream); } } var response = ResponseModelFactory.CreateInstance; //using (_dbContext) //{ // ProcessPictureViewModel picture = new ProcessPictureViewModel(); // picture.PicturePath = strpath; // picture.IsDelete = "0"; // picture.AddTime = DateTime.Now.ToString("MMddHHmmss"); // if (_dbContext.SystemSetting.Count(x => x.IsDelete != "1") > 0) // { // var entity = _dbContext.SystemSetting.FirstOrDefault(x => x.IsDelete != "1"); // entity.PicturePath = picture.PicturePath; // entity.IsDelete = picture.IsDelete; // entity.AddTime = picture.AddTime; // _dbContext.SaveChanges(); // } // else // { // var entity = _mapper.Map<ProcessPictureViewModel, SystemSetting>(picture); // entity.SystemSettingUuid = Guid.NewGuid(); // entity.PicturePath = picture.PicturePath; // entity.IsDelete = picture.IsDelete; // entity.AddTime = picture.AddTime; // _dbContext.SystemSetting.Add(entity); // _dbContext.SaveChanges(); // } //} data.Code = "200"; data.Path = strimage; data.Msg = "上传成功"; return(data); } else { data.Code = "404"; data.Msg = "图片过大"; return(data); } } else { data.Code = "404"; data.Msg = "图片格式错误"; return(data); } }