Problems with CreateImage() and uploading incorrect images - Bugs & Issues - Bugs & Issues - Progress Community

Problems with CreateImage() and uploading incorrect images

 Bugs & Issues

Problems with CreateImage() and uploading incorrect images

  • Problems with CreateImage() and uploading incorrect images
  • 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 

  • 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