private void VerifyData(ArtMediaType artMediaType) { string[] limitTypes = { ".jpg", ".bmp", ".png", ".jpeg", ".gif" }; if (artMediaType == ArtMediaType.Pic) { foreach (var file in Request.Form.Files) { if (file.Length > 1024 * 1024 * 5) { throw new EduException("文件不能大于5M"); } var ext = Path.GetExtension(file.FileName).ToLower(); if (!limitTypes.Contains(ext)) { throw new EduException($"不支持的文件扩展名{ext}"); } } } else { limitTypes = new string[] { ".mp4", ".mov", ".avi", ".3gp" }; foreach (var file in Request.Form.Files) { var ext = Path.GetExtension(file.FileName).ToLower(); if (!limitTypes.Contains(ext)) { throw new EduException($"不支持的文件扩展名{ext}"); } } } }
public ResultNormal UploadArt(long artId, ArtMediaType ArtMediaType, int no, bool isLast = false) { ResultNormal result = new ResultNormal(); // List<EArtDetail> detailList = new List<EArtDetail>(); try { if (artId <= 0) { throw new EduException("活动编号没有获取!"); } if (Request.Form != null && Request.Form.Files.Count > 0) { VerifyData(ArtMediaType); var file = Request.Form.Files[0]; var ext = Path.GetExtension(file.FileName).ToLower(); var fileName = $"art_{artId}_{no}_{DateTime.Now.ToString("hhmmss")}{ext}"; var date = DateTime.Now.ToString("yyyy_MM_dd"); DirectoryInfo di = new DirectoryInfo(EduConfig.UploadArtRoot + date); if (!di.Exists) { di.Create(); } var filepath = di.FullName + "\\" + fileName; using (FileStream fs = new FileStream(filepath, FileMode.Create)) { file.CopyTo(fs); fs.Flush(); } EArtDetail detail = new EArtDetail { ArtId = artId, FilePath = EduEnviroment.VirPath_ArtRoot + date + "/" + fileName, }; _WxMiniSrv.AddArtDetail(detail); if (isLast) { var art = _WxMiniSrv.GetArtInfo(artId); art.RecordStatus = RecordStatus.Normal; art.CoverFilePath = detail.FilePath; } _WxMiniSrv.SaveChanges(); } else { throw new EduException("没有上传的作品"); } } catch (EduException eex) { result.ErrorMsg = eex.Message; } catch (Exception ex) { result.ErrorMsg = "上传失败"; NLogHelper.ErrorTxt($"UploadArt:{ex.Message}"); NLogHelper.ErrorTxt($"UploadArt:{ex.InnerException.Message}"); } return(result); }