Problems with CreateImage() and uploading incorrect images

Posted by Community Admin on 04-Aug-2018 19:31

Problems with CreateImage() and uploading incorrect images

All Replies

Posted by Community Admin on 15-Jul-2015 00:00

Hello all, I've got a really weird issue. I'm trying to upload a set of image files programmatically and I'm was running into a problem where CreateImage() was creating Image objects with the same Guid each time called, even with calling .SaveChanges() in between. I resorted to making a new Guid manually and calling CreateImage() with the new Guid. That problem was solved. Now, when I upload an image, regardless of what file I send, it uploads the same image. This is my code:

 

 

public Guid ImportImage(String fileName, String title)
       
            var im = lm.CreateImage(Guid.NewGuid());
            im.Title = title;
            im.Parent = lm.GetAlbums().FirstOrDefault();
            lm.Upload(im, File.Open(baseDirectoryURL + fileName, FileMode.Open, FileAccess.Read), ".jpg");
            lm.RecompileItemUrls(im);
            lm.Lifecycle.Publish(im);
            lm.SaveChanges();
            return im.Id;
       

 

 This method is called multiple times in a for loop, each time with a different filename. Ex.

foreach (String f in filenames)

     ImportImage(f, "Title"); 

But it only seems to upload whatever the first file is each time. Furthermore, the thumbnail is also completely different from the file. Can anyone help?? Thanks 

Posted by Community Admin on 20-Jul-2015 00:00

Hi,

You can try the code snippet bellow in order to import an image;

  private Telerik.Sitefinity.Libraries.Model.Image UploadImage(string imagePath)
 
     LibrariesManager librariesManager = LibrariesManager.GetManager();
     Telerik.Sitefinity.Libraries.Model.Image image = librariesManager.CreateImage(Guid.NewGuid());
  
     Album album = librariesManager.GetAlbums().SingleOrDefault(x => x.UrlName == "mylybraryurlname");
     image.Parent = album;
     image.Title = "ImageTitle";
     image.DateCreated = DateTime.UtcNow;
     image.PublicationDate = DateTime.UtcNow;
     image.LastModified = DateTime.UtcNow;
     image.UrlName = Regex.Replace(image.Title.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
  
     //Recompiles and validates the url of the Image.
     librariesManager.RecompileAndValidateUrls(image);
  
     FileStream fileStream = new FileStream(imagePath, FileMode.Open);
  
     string extension = Path.GetExtension(imagePath);
     //Upload the Image
     librariesManager.Upload(image, fileStream, extension);
  
     //Save the changes.
     librariesManager.SaveChanges();
  
     //Publish the Image item. The live version acquires new ID.
     var bag = new Dictionary<string, string>();
     bag.Add("ContentType", typeof(Image).FullName);
     WorkflowManager.MessageWorkflow(image.Id, typeof(Image), null, "Publish", false, bag);
  
     return image;
 

I hope it was useful.

Regards,
Svetoslav Manchev
Telerik
 
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
 

This thread is closed