Guest User

Untitled

a guest
Jul 22nd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. protected void btnUpload_Click(object sender, EventArgs e)
  2. {
  3. // If no file has been selected, leave the function
  4. if (uploader.FileBytes.Length == 0 || string.IsNullOrEmpty(uploader.FileName))
  5.  
  6. return;
  7.  
  8.  
  9. // Granting privileges
  10. SPSecurity.RunWithElevatedPrivileges(delegate
  11. {
  12. Guid guid = Guid.NewGuid();
  13.  
  14. // Picture Library
  15. SPPictureLibrary list;
  16.  
  17. try
  18. {
  19. // Retrieve a reference to the picture library
  20. list = (SPPictureLibrary)Site.RootWeb.Lists["Image Picker Picture List"];
  21. }
  22. catch
  23. {
  24. // If the library "Image Picker Picture Library" doesn't exists, create the list
  25. Guid listGuid = Site.RootWeb.Lists.Add("Image Picker Picture List", SPUtility.GetLocalizedString("$Resources:ListDescription", "ImagePicker", SPContext.Current.Web.Language), SPListTemplateType.PictureLibrary);
  26.  
  27. list = (SPPictureLibrary)Site.RootWeb.Lists[listGuid];
  28. list.OnQuickLaunch = false;
  29. list.Update();
  30. }
  31.  
  32. // Add the picture in the library
  33. SPFile it = list.RootFolder.Files.Add(guid + uploader.FileName.Substring(uploader.FileName.LastIndexOf('.')), uploader.FileBytes, true);
  34. // Update the item
  35. it.Update();
  36.  
  37. // Create the Bitmap from the uploaded picture
  38. Bitmap image = new Bitmap(uploader.FileContent);
  39.  
  40. // Get the dimension of the miniature
  41. Dimension dimensions = Resizer.GetMiniature(image.Width, image.Height);
  42.  
  43. // Changing the picture of the parent page
  44. Page.Response.Write(@"<script language='javascript'>
  45. opener.document.getElementById('" + Page.Request.QueryString["GUID"] + @"').src = '" + list.ParentWeb.Site.Url + "/" + it.Url + @"';
  46. opener.document.getElementById('" + Page.Request.QueryString["GUID"] + @"').style.visibility = 'visible';
  47. opener.document.getElementById('" + Page.Request.QueryString["GUID"] + @"').style.width = '" + dimensions.Width + @"px';
  48. opener.document.getElementById('" + Page.Request.QueryString["GUID"] + @"').style.height = '" + dimensions.Height + @"px';
  49. opener.document.getElementById('" + Page.Request.QueryString["path"] + "').value = '" + list.ParentWeb.Site.Url + "/" + it.Url + @"'
  50. window.close();
  51. </script>");
  52. });
  53. }
Add Comment
Please, Sign In to add comment