Page images getting deleted on publish

Ashutosh Pandey asked on June 8, 2020 14:44

We have a Kentico 10 based site.

We are creating pages based on custom page type Resource

We have a field named PDFImage using Direct Uploader control in the Resource page type.

On the web pages, we are displaying images like this:

<img src='/CMSPages/GetFile.aspx?width=61&guid=<%#Eval("PDFImage")%>' alt='<%# Eval("DocumentName")%>'/>

This generates images and renders based on size.

The problem is, if I checkout the page and publish again, all the images associated with PDFImage field are deleted and do not render next time.

I'm not able to understand this behavior.

If I checkout and set the image second time and publish, it displays the image.

Can you help me understanding the above behavior?

I checked there is no manual event behind the scene that is deleting the images.

Edit

Based on questions asked:

  1. We are storing images on file system
  2. The project is not upgraded
  3. Yes, we are using workflows

Correct Answer

Juraj Ondrus answered on June 9, 2020 07:06

What is the history of the project? Was it upgraded or deployed some time ago? Are you using workflows? If yes, where are you storing the files? File system or database?
Isn't it possible that you are missing the files version history, in case you are storing files on the file system?
Check whether you have the binary data available here: *\CMS\App_Data\VersionHistory\Attachments* if not, you can copy them from the original project backups, if you still have them.
Another cause could be that for some reason the site's code name was changed - this means when storing file on file system, the site's folder name does not match the actual site's code name.
Another option is to re-upload the images to start new version history.
f the backups are no longer available, and the content of the \CMS\App_Data\VersionHistory\Attachments\ cannot be restored, you can fix the AttachmentHistory objects using the following script, which will copy the AttachmentBinary from the latest published version and re-create the physical files. The old images will be lost, but at least you can keep all the history and this way you can fix all the images in the system at once:

public void FixAttachmentHistories()
{
    // Get the list of all attachment, load only necessary columns for good performance
    var allAttachments = AttachmentInfoProvider.GetAttachments()
        .Columns("AttachmentID", "AttachmentDocumentID", "AttachmentGUID");
    foreach (var attachment in allAttachments)
    {
        // Get all history entries for this attachment
        var allHistory = AttachmentHistoryInfoProvider.GetAttachmentHistories()
            .WhereEquals("AttachmentGUID", attachment.AttachmentGUID);
        foreach (var history in allHistory)
        {
            // Check if the binary is missing
            if (history.AttachmentBinary == null)
            {
                // Let's copy the binary from the published version
                DocumentAttachment attachmentWithBinary = (DocumentAttachment)AttachmentInfoProvider.GetAttachmentInfo(attachment.AttachmentID, true);
                var data = AttachmentBinaryHelper.GetAttachmentBinary(attachmentWithBinary);
                history.AttachmentBinary = data;
                history.Update();
            }
        }
    }
}

P.S.: To render and display images, you can use one of the available transformation methods. For example - <%# GetImage("ImageFieldName") %> anmd it has also overloads with size parameters.

1 votesVote for this answer Unmark Correct answer

Recent Answers


Brenden Kehren answered on June 8, 2020 15:07

Can you confirm it is physically removing the image from the file system or the database? Could it simply be a caching issue?

0 votesVote for this answer Mark as a Correct answer

David te Kloese answered on June 8, 2020 15:41

Does <%#Eval("PDFImage")%> render anything? As it perhaps not properly saves the value?

1 votesVote for this answer Mark as a Correct answer

Ashutosh Pandey answered on June 8, 2020 15:50

Yes, the file is actually getting deleted on publish. Then I go upload file again and publish, it works fine.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.