//对所有没有封面且有图片的活动,找到活动中第一张图,裁剪为270*200保存为封面,并保存到本地 private void setActivityCover(VolunteerService myService) { IEnumerable <Activity> allActivities = myService.FindAllActivities("", "", false, 0, 0); foreach (Activity activity in allActivities) { if (activity.Cover == null) { if (activity.Photos.Count > 0) { try { string firstPhotoPath = HttpContext.Current.Server.MapPath("~" + activity.Photos.FirstOrDefault()); Stream fileStream = new FileStream(firstPhotoPath, FileMode.Open); string imageName = Guid.NewGuid().ToString(); //生成图像名称 string path = "/Static/Images/Activity/" + imageName + ".jpg"; //相对路径+图像名称+图像格式 string filePath = HttpContext.Current.Server.MapPath("~" + path); //绝对路径 activity.Cover = path; //270*200 HandleImageService.CutForCustom(fileStream, filePath, 270, 200, 75); } catch { activity.Cover = "/Static/Images/Activity/default.jpg"; } activity.Save(); } else { activity.Cover = "/Static/Images/Activity/default.jpg"; activity.Save(); } } } }
public HttpResponseMessage UploadActivityImage() { if (ValidationService.AuthorizeToken(GetToken(), "post:/api/content/uploadactivityimage") == false) { return(new HttpResponseMessage { StatusCode = HttpStatusCode.Unauthorized, Content = new StringContent("无访问权限", System.Text.Encoding.GetEncoding("UTF-8"), "application/text") }); } var httpRequest = HttpContext.Current.Request; if (httpRequest.Files.Count != 1) { return(new HttpResponseMessage { StatusCode = HttpStatusCode.BadRequest, Content = new StringContent("error", System.Text.Encoding.GetEncoding("UTF-8"), "application/text") }); } var postedFile = httpRequest.Files[0]; string imageName = Guid.NewGuid().ToString(); //生成图像名称 string path = "/Static/Images/Activity/" + imageName + postedFile.FileName.Substring(postedFile.FileName.LastIndexOf(".")); //相对路径+图像名称+图像格式 string filePath = HttpContext.Current.Server.MapPath("~" + path); //绝对路径 Stream fileStream = postedFile.InputStream; HandleImageService.CutForCustom(fileStream, filePath, 960, 720, 75);//剪裁为960*720并保存图像到本地 fileStream.Close(); return(new HttpResponseMessage { StatusCode = HttpStatusCode.Created, Content = new StringContent(path, System.Text.Encoding.GetEncoding("UTF-8"), "application/text") }); }
public HttpResponseMessage UploadAvatar() { Guid userId = new Guid(User.Identity.GetUserId()); OldHouseUser user = MyService.MyUserManager.FindByIdAsync(userId).Result; var httpRequest = HttpContext.Current.Request; if (httpRequest.Files.Count != 1) { return(new HttpResponseMessage { StatusCode = HttpStatusCode.BadRequest, Content = new StringContent("请求不合法", System.Text.Encoding.GetEncoding("UTF-8"), "application/json") }); } var postedFile = httpRequest.Files[0]; string path = user.Id.ToString() + postedFile.FileName.Substring(postedFile.FileName.LastIndexOf(".")); //相对路径+图像名称+图像格式 string filePath = HttpContext.Current.Server.MapPath("~/Content/Images/avatar/" + path); //绝对路径 string oldAvatarPath = HttpContext.Current.Server.MapPath("~/Content/Images/avatar/" + user.Avatar); //根据相应Profile获得原图像相对路径 if (File.Exists(oldAvatarPath) && !oldAvatarPath.ToLower().Contains("default")) //如果原图像存在且不为系统默认头像,则删除 { File.Delete(oldAvatarPath); } Stream fileStream = postedFile.InputStream; HandleImageService.CutForCustom(fileStream, filePath, 256, 256, 75);//剪裁为256*256并保存图像到本地 fileStream.Close(); user.Avatar = path;//将新图像相对路径保存到当前用户的相应Profile中 MyService.MyUserManager.UserRepository.SaveOne(user); return(new HttpResponseMessage(HttpStatusCode.Created)); }
public HttpResponseMessage UploadAvatar() { if (ValidationService.AuthorizeToken(GetToken(), "post:/api/content/uploadavatar") == false) { return(new HttpResponseMessage { StatusCode = HttpStatusCode.Unauthorized, Content = new StringContent("无访问权限", System.Text.Encoding.GetEncoding("UTF-8"), "application/text") }); } User CurrentUser = ValidationService.FindUserWithToken(GetToken()); var httpRequest = HttpContext.Current.Request; if (httpRequest.Files.Count != 1) { return(new HttpResponseMessage { StatusCode = HttpStatusCode.BadRequest, Content = new StringContent("请求不合法", System.Text.Encoding.GetEncoding("UTF-8"), "application/json") }); } string imageName = Guid.NewGuid().ToString(); //生成图像名称 var postedFile = httpRequest.Files[0]; string path = "/Static/Images/Avatar/" + imageName + postedFile.FileName.Substring(postedFile.FileName.LastIndexOf(".")); //相对路径+图像名称+图像格式 string filePath = HttpContext.Current.Server.MapPath("~" + path); //绝对路径 Stream fileStream = postedFile.InputStream; HandleImageService.CutForCustom(fileStream, filePath, 128, 128, 75);//剪裁为128*128并保存图像到本地 fileStream.Close(); string role = Enum.GetName(typeof(Role), CurrentUser.UserRole.FirstOrDefault()); //获取角色名称 string oldAvatarPath = HttpContext.Current.Server.MapPath("~" + CurrentUser.UserProfiles[CurrentUser.Name + role + "Profile"].Avatar.AvatarPath); //根据相应Profile获得原图像相对路径 if (File.Exists(oldAvatarPath) && !oldAvatarPath.Contains("default.jpg")) //如果原图像存在且不为系统默认头像,则删除 { File.Delete(oldAvatarPath); } CurrentUser.UserProfiles[CurrentUser.Name + role + "Profile"].Avatar.AvatarPath = path;//将新图像相对路径保存到当前用户的相应Profile中 CurrentUser.Save(); //生成ChangeAvatarEvent EventService.Publish("ChangeAvatarEvent", null, CurrentUser.Id); return(new HttpResponseMessage(HttpStatusCode.Created)); }
public IHttpActionResult UploadHouseImage() { var httpRequest = HttpContext.Current.Request; if (httpRequest.Files.Count != 1) { return(Ok(new { error = "上传失败" })); } var postedFile = httpRequest.Files[0]; string imageName = Guid.NewGuid().ToString(); //生成图像名称 string path = "/content/Images/house/" + imageName + postedFile.FileName.Substring(postedFile.FileName.LastIndexOf(".")); //相对路径+图像名称+图像格式 string filePath = HttpContext.Current.Server.MapPath("~" + path); //绝对路径 Stream fileStream = postedFile.InputStream; HandleImageService.CutForCustom(fileStream, filePath, 960, 720, 75);//剪裁为960*720并保存图像到本地 fileStream.Close(); return(Ok(new UploadResponse { UploadedFileUrls = new List <string> { path } })); }