# Replace image via code

**URL:** <https://forum.litium.com/t/replace-image-via-code/3477>\
**Category:** Questions\
**Created:** [April 7, 2025, 7:35am UTC](https://forum.litium.com/t/replace-image-via-code/3477 "2025-04-07T07:35:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Spotonkastebo](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/spotonkastebo/32/717_2.png) [@Spotonkastebo](https://forum.litium.com/u/Spotonkastebo)\
**Post date:** [April 7, 2025, 7:35am UTC](https://forum.litium.com/t/replace-image-via-code/3477/1 "2025-04-07T07:35:43Z")

</div>

When we upload a image to litium media module we have a broker that is supposed to check if the file already exists and if so replace the file. Since we want to keep existsing links to the file we want to have the same behaviour as if a duplicate file was uploaded via lite backoffice.

How do one copy the blob from the new file to the old file blob?

We have tried looking at blobservice and blobextensions to get the blobs and copy from the new file to the new but not sure if this is the wrong approach or if we done something wrong since the old file is not updated in litium backoffice even if the code runs without errors.

```auto
                var blobContainer = blobService.Create(File.BlobAuthority);

                using (securityContextService.ActAsSystem(nameof(FileBroker)))
                {
                    var newFileBlobContainer = blobService.Get(newfile.BlobUri);
                    var newFileBlob = newFileBlobContainer.GetDefault();

                    var existingFileClone = existingFile.MakeWritableClone();
                    var existingFileBlobContainer = blobService.Get(existingFileClone.BlobUri);
                    var existingFileBlob = existingFileBlobContainer.GetDefault();

                    newFileBlob.CopyTo(existingFileBlob);
                    // existingFileClone.BlobUri = existingFile.BlobUri;
                    existingFileClone.FileSize = newfile.FileSize;
                    fileService.Update(existingFileClone);

                    fileService.Delete(file);
                }
            

```

Litium version: 8.20.3

---

<div class="post-metadata">

**Author:** ![patric.forsgard](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/patric.forsgard/32/10_2.png) [@patric.forsgard](https://forum.litium.com/u/patric.forsgard)\
**Post date:** [April 7, 2025, 8:12am UTC](https://forum.litium.com/t/replace-image-via-code/3477/2 "2025-04-07T08:12:47Z")

</div>

If you instead of working directly with the blobs using the update method as

```auto
FileService fileService = ...;
await fileService.Update(file, newFileName, newFileStream)

```

The the blob for the file-entity will be updated and all existing links for the file entity is unchanged but will point to the new file instead.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex030/uploads/litium/original/1X/12eddac3b8b661de1db86af10ac8151b571e6fb9.png) [@system](https://forum.litium.com/u/system)\
**Post date:** [May 5, 2025, 8:13am UTC](https://forum.litium.com/t/replace-image-via-code/3477/3 "2025-05-05T08:13:14Z")

</div>

Closed on May 5, 2025, 8:13am UTC

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
