public string UpdateVoteUsaImage(string politicianKey, string url) { var request = (HttpWebRequest)WebRequest.Create(url); var response = (HttpWebResponse)request.GetResponse(); using (var inputStream = response.GetResponseStream()) using (var memoryStream = new MemoryStream()) { Debug.Assert(inputStream != null, "inputStream != null"); inputStream.CopyTo(memoryStream); byte[] originalBlob; Size originalSize; if (PoliticiansImagesBlobs.GetHeadshot100(politicianKey) == null) { ImageManager.UpdateAllPoliticianImages(politicianKey, memoryStream, DateTime.UtcNow, true, out originalSize, out originalBlob); } else { ImageManager.UpdatePoliticianProfileImages(politicianKey, memoryStream, DateTime.UtcNow, true, out originalSize, out originalBlob); } } return(VotePage.GetPoliticianImageUrl(politicianKey, 300, true)); }
protected void ButtonUploadPictureProfile_ServerClick(object sender, EventArgs e) { try { var postedFile = Request.Files[ImageFileProfile.Name]; var politicianKey = LabelPoliticianKey.Text; var uploadTime = DateTime.UtcNow; if ((postedFile == null) || (postedFile.ContentLength == 0)) { return; } Size originalSize; ImageManager.UpdatePoliticianProfileImages(politicianKey, postedFile.InputStream, uploadTime, out originalSize); CommonCacheInvalidation.ScheduleInvalidation("politicianimage", politicianKey); // We only want to propagate the profile to the headshot if there // is no current Headshot -- we check Headshot100 if (PoliticiansImagesBlobs.GetHeadshot100(politicianKey) == null) { ImageManager.UpdateResizedPoliticianHeadshotImages(politicianKey, postedFile.InputStream, uploadTime, out originalSize); } var memoryStream = new MemoryStream(); postedFile.InputStream.Position = 0; postedFile.InputStream.CopyTo(memoryStream); postedFile.InputStream.Position = 0; var imageBlob = memoryStream.ToArray(); LogPoliticiansImagesOriginal.Insert(politicianKey, imageBlob, uploadTime, UserSecurityClass, UserName, 0); UpdateRowCountAfterChange(); Msg.Text = Ok("The profile image for ID " + politicianKey + " was uploaded."); } catch (Exception ex) { HandleException(ex); } }
protected void Page_Load(object sender, EventArgs e) { var result = new AjaxResponse(); try { // assume success unless an exception is thrown result.Success = true; // copy back the groupname and uploadid result.GroupName = Request.Form["groupname"]; result.UploadId = Request.Form["uploadid"]; // there should always be exactly one file if (Request.Files.Count == 0) { throw new VoteException("The upload file is missing"); } if (Request.Files.Count > 1) { throw new VoteException("Unexpected files in the upload package"); } // Test error handling //throw new VoteException("Some weird-ass error"); // get the file var postedFile = Request.Files[0]; // save the timestamp so the time we post to all the various tables matches // exactly... var uploadTime = DateTime.UtcNow; // We only update headshot images if there is no Headshot -- // we check Headshot100 determine if this is so // We request duplicate testing... byte[] blob; Size originalSize; byte[] originalBlob; if (PoliticiansImagesBlobs.GetHeadshot100(PoliticianKey) == null) { blob = ImageManager.UpdateAllPoliticianImages(PoliticianKey, postedFile.InputStream, uploadTime, true, out originalSize, out originalBlob); } else { blob = ImageManager.UpdatePoliticianProfileImages(PoliticianKey, postedFile.InputStream, uploadTime, true, out originalSize, out originalBlob); } if (blob == null) // means it was a duplicate { result.Duplicate = true; result.Message = "The uploaded picture is the same as we already have on our servers."; } else { result.Message = "The picture uploaded successfully. Original file length = " + postedFile.InputStream.Length.ToString(CultureInfo.InvariantCulture); result.Length = postedFile.InputStream.Length; result.Width = originalSize.Width; result.Height = originalSize.Height; // invalidate cached image on load-balanced servers CommonCacheInvalidation.ScheduleInvalidation("politicianimage", PoliticianKey); LogImageChange(originalBlob, blob, uploadTime); Politicians.UpdateDatePictureUploaded(uploadTime, PoliticianKey); } } catch (Exception ex) { result.Success = false; result.Message = ex.Message; } WriteJsonResultToResponse(result); }