/// <summary> /// 儲存使用者圖示 /// </summary> /// <param name="httpPostedFile">傳遞的檔案</param> /// <param name="userID">儲存檔案名稱</param> public static void SaveUserPic(HttpPostedFileBase httpPostedFile, int userID) { try { string realFileViturePath = $"UserIcon/UserIcon_{userID}{Path.GetExtension(httpPostedFile.FileName)}"; string tmpFilePath = HttpContext.Current.Server.MapPath($@"~/UserIcon/2_UserIcon{userID}{Path.GetExtension(httpPostedFile.FileName)}"); string realFilePath = HttpContext.Current.Server.MapPath($@"~/{realFileViturePath}"); if (File.Exists(tmpFilePath)) { File.Delete(HttpContext.Current.Server.MapPath(tmpFilePath)); } httpPostedFile.SaveAs(tmpFilePath); using (Image originImg = Image.FromFile(tmpFilePath)) { using (var formattedImg = AddFrame(GetResizeImage(originImg, 100, 100), 100, 100)) { formattedImg.Save(realFilePath); } } if (File.Exists(tmpFilePath)) { File.Delete(tmpFilePath); MessageBoardEntities messageBoardEntities = new MessageBoardEntities(); var user = messageBoardEntities.UserList.Find(userID); user.UserIcon = realFileViturePath; messageBoardEntities.SaveChanges(); } GC.Collect(); } catch (Exception err) { LogTool.DoErrorLog($"#{DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff")}:{err.Message}\r\n{err.StackTrace}\r\n"); throw; } }
/// <summary> /// 儲存使用者 Log 檔 /// </summary> /// <param name="userID">使用者 ID</param> /// <param name="userOperator">訊息</param> private void DoUserLog(int userID, string userOperator) { try { UserLog userLog = new UserLog { UserID = userID, UserOperator = userOperator, IP = PBTool.GetIP(), CreateDate = DateTime.Now }; messageBoardEntities.UserLog.Add(userLog); messageBoardEntities.SaveChanges(); // ToDo 20180817 操作紀錄 //var userJSON = JsonConvert.SerializeObject(userLog); } catch (Exception err) { LogTool.DoErrorLog($"#{DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff")}:{err.Message}\r\n"); throw; } }
public static void SaveMessagePic(HttpPostedFileBase httpPostedFile, int userID, int messageID) { try { string fileName = Guid.NewGuid().ToString(); string realFileViturePath = $"messagePics/origin/{fileName}{Path.GetExtension(httpPostedFile.FileName)}"; string tmpFilePath = HttpContext.Current.Server.MapPath($@"~/messagePics/{fileName}{Path.GetExtension(httpPostedFile.FileName)}"); string realFilePath = HttpContext.Current.Server.MapPath($@"~/{realFileViturePath}"); httpPostedFile.SaveAs(realFilePath); using (Image originImg = Image.FromFile(realFilePath)) { using (var formattedImg = GetResizeImage(originImg, 60, 60)) { formattedImg.Save(tmpFilePath); } } MessageBoardEntities messageBoardEntities = new MessageBoardEntities(); MessagePic messagePic = new MessagePic() { CreateDate = DateTime.Now, MessageID = messageID, CreateUserID = userID, PicURL = realFileViturePath, picStatus = true }; messageBoardEntities.MessagePic.Add(messagePic); messageBoardEntities.SaveChanges(); GC.Collect(); } catch (Exception err) { LogTool.DoErrorLog($"#{DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff")}:{err.Message}\r\n{err.StackTrace}\r\n"); } }