.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> SharePoint >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Update Picture Using Custom Code

Posted By:Sagar Pardeshi       Posted Date: February 10, 2011    Points: 15    Category: SharePoint    URL: http://www.dotnetspark.com  

Update Picture Using Custom Code
 

	protected void Clicked(object sender, EventArgs e)
	{		
		
		if (FileUpload1.HasFile)
            {
                String fileExt = Path.GetExtension(FileUpload1.FileName).ToLower();
               
              
                if (fileExt == ".jpg" || fileExt == ".png" || fileExt == ".jpeg") // ".gif" ".png"
                {
                   
                    try
                    {
                    	SPSecurity.RunWithElevatedPrivileges(delegate()
						{
							Site = SPContext.Current.Site;
                        	StreamImage = FileUpload1.PostedFile.InputStream;
                       
                        	using (SPWeb Web = Site.OpenWeb())
                        	{
                        		Username = Web.CurrentUser.LoginName;
                        		NewUserName = new StringBuilder(Username);
                        		Web.AllowUnsafeUpdates = true;
                        	    List = Web.Lists["Picturelib"];                            
                            /*
                        	    if (Username.IndexOf("i:0#.f|aspnetsqlmembershipprovider|") != -1)
								{			
									NewUserName.Replace("i:0#.f|aspnetsqlmembershipprovider|", "");			
								}
								else if (Username.IndexOf("i:0#.w|"+servername+"") != -1)
								{			
									NewUserName.Replace("i:0#.w|"+servername+"", "");			
								}
                        	    //Page.ClientScript.RegisterStartupScript(GetType(), "message", "alert('"+NewUserName+"');", true);
	                             */
	                            oSPFile = List.RootFolder.Files.Add(FileUpload1.FileName + fileExt, StreamImage, true);
								//oSPFile = List.RootFolder.Files.Add(NewUserName+ fileExt, StreamImage, true);
                        	    
                        	    oSPFile.Update();
                            
                        	    Web.AllowUnsafeUpdates = false;                            

                        	    //MyPath = Site.Url + "/" + List.ToString() + "/" +  Username.Substring(35) + fileExt;
                        	    MyPath = Site.Url + "/" + List.ToString() + "/" +  FileUpload1.FileName+ fileExt;
                        	    //System.Drawing.Image fFileWidthAndHeight = System.Drawing.Image.FromFile(MyPath);
  
                        	    Page.ClientScript.RegisterStartupScript(GetType(), "Script", "var MyPhotoPath = '"+MyPath+"'; document.getElementById('ctl00_PlaceHolderMain_g_7cc13daa_dd5d_4f6d_8a00_ac91ed25df1e_ff7_1_ctl00_ctl00_UrlFieldUrl').value = MyPhotoPath;document.getElementById('MyPhoto').src = MyPhotoPath;", true);
                        	    
                        	}
                        });
                    }
                    catch (Exception ex)
                    {
                    	GenerateErrorLog(ex.Message.ToString());
                    	//Page.ClientScript.RegisterStartupScript(GetType(), "message", "alert('"+ ex.Message.ToString() +"');", true);
                       // Page.ClientScript.RegisterStartupScript(GetType(), "message","   
 

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend