// todo :上传附件 public bool uploadAttach(string name, long userId, long noteId, out string msg, out long serverFileId) { msg = ""; serverFileId = 0; var uploadDirPath = $"{RuntimeEnvironment.DirectorySeparatorChar}www/attachs/{userId.ToString("x")}/images/{DateTime.Now.ToString("yyyy_MM")}/"; if (RuntimeEnvironment.IsWindows) { uploadDirPath = $@"upload\{userId.ToString("x")}\attachs\{DateTime.Now.ToString("yyyy_MM")}\"; } var diskFileId = SnowFlake_Net.GenerateSnowFlakeID(); serverFileId = diskFileId; var httpFiles = _accessor.HttpContext.Request.Form.Files; //检查是否登录 if (userId == 0) { userId = GetUserIdBySession(); if (userId == 0) { msg = "NoLogin"; return(false); } } if (httpFiles == null || httpFiles.Count < 1) { return(false); } var httpFile = httpFiles[name]; var fileEXT = Path.GetExtension(httpFile.FileName).Replace(".", ""); if (!IsAllowAttachExt(fileEXT)) { msg = $"The_Attach_extension_{fileEXT}_is_blocked"; return(false); } var fileName = diskFileId.ToString("x") + "." + fileEXT; //判断合法性 if (httpFiles == null || httpFile.Length < 0) { return(false); } //将文件保存在磁盘 Task <bool> task = SaveUploadFileOnDiskAsync(httpFile, uploadDirPath, fileName); bool result = task.Result; if (result) { //将结果保存在数据库 AttachInfo attachInfo = new AttachInfo() { AttachId = diskFileId, UserId = userId, NoteId = noteId, UploadUserId = userId, Name = fileName, Title = httpFile.FileName, Size = httpFile.Length, Path = uploadDirPath + fileName, Type = fileEXT.ToLower(), CreatedTime = DateTime.Now //todo: 增加特性=图片管理 }; var AddResult = AttachService.AddAttach(attachInfo, true, out string AttachMsg); if (!AddResult) { msg = "添加数据库失败"; } return(AddResult); } else { msg = "磁盘保存失败"; return(false); } }
protected bool UploadAttach(string name, long?userId, long?noteId, out string msg, out long?serverFileId) { msg = ""; serverFileId = 0; FileStoreConfig config = configFileService.WebConfig.FileStoreConfig; var diskFileId = idGenerator.NextId(); serverFileId = diskFileId; var httpFiles = _accessor.HttpContext.Request.Form.Files; //检查是否登录 if (userId == 0) { userId = GetUserIdBySession(); if (userId == 0) { msg = "NoLogin"; return(false); } } if (httpFiles == null || httpFiles.Count < 1) { return(false); } var httpFile = httpFiles[name]; var fileEXT = Path.GetExtension(httpFile.FileName).Replace(".", ""); if (!IsAllowAttachExt(fileEXT)) { msg = $"The_Attach_extension_{fileEXT}_is_blocked"; return(false); } var fileName = diskFileId.ToHex() + "." + fileEXT; //判断合法性 if (httpFiles == null || httpFile.Length < 0) { return(false); } //将文件保存在磁盘 // Task<bool> task = noteFileService.SaveUploadFileOnUPYunAsync(upyun, httpFile, uploadDirPath, fileName); //Task<bool> task = noteFileService.SaveUploadFileOnDiskAsync(httpFile, uploadDirPath, fileName); var ext = Path.GetExtension(fileName); var provider = new FileExtensionContentTypeProvider(); var memi = provider.Mappings[ext]; var nowTime = DateTime.Now; var objectName = $"{userId.ToHex()}/attachments/{ nowTime.ToString("yyyy")}/{nowTime.ToString("MM")}/{diskFileId.ToHex()}{ext}"; bool result = noteFileService.SaveFile(objectName, httpFile, memi).Result; if (result) { //将结果保存在数据库 AttachInfo attachInfo = new AttachInfo() { AttachId = diskFileId, UserId = userId, NoteId = noteId, UploadUserId = userId, Name = fileName, Title = httpFile.FileName, Size = httpFile.Length, Path = fileName, Type = fileEXT.ToLower(), CreatedTime = DateTime.Now //todo: 增加特性=图片管理 }; var AddResult = attachService.AddAttach(attachInfo, true, out string AttachMsg); if (!AddResult) { msg = "添加数据库失败"; } return(AddResult); } else { msg = "磁盘保存失败"; return(false); } }