Imported image attachments not resolving on frontend.

James Mosquito asked on April 19, 2021 19:10

Hi,

Using Kentico 12SP 12.0.93 we are attaching images to product pages in a ScheduledTask using:

// Delete Existing
var attachment = DocumentHelper.GetAttachment(productPage, productImage.FileInfo?.Name);

if (attachment != null)
{
    DocumentHelper.DeleteAttachment(productPage, attachment.AttachmentGUID);
}

// Add New
var attachmentGuid = Guid.NewGuid();
var fieldGuid = Guid.Parse(PRODUCT_IMAGES_FIELD_GUID);
DocumentHelper.AddGroupedAttachment(productPage, attachmentGuid, fieldGuid, productImage.FilePath);

Two issue we are encountering include:

  1. When retrieving the image on the frontend using: @Url.Kentico().ImageUrl(documentImage.GetPath(), SizeConstraint.MaxWidthOrHeight(430));
    The image url looks okay e.g. https://domain.com/getattachment/<attachment-guid>/<image-filename>.jpg?maxsidesize=430&resizemode=force
    however we get a 404 error. The attachment GUID and filename match in the database and I can locate the files with matching guid's on the filesystem.

  2. Running the same task again duplicates the image attachments listed in the attachment field on the page document with the filename suffixed by (1) etc.

Thanks, James

Recent Answers


Brenden Kehren answered on April 23, 2021 21:10

For v12 I'm assuming you're following these instructions. Paying close attention to the PRODUCT_IMAGES_FIELD_GUID field. The documentation states // Navigate to the 'Page types' application -> Select the Page type -> 'Fields' tab -> Select the field, you get the GUID from that field.

This is assuming you have a field on the page type to accept those attachments and it is using the Attachments Control as the form control. If you're not doing this then you'll get 404 errors because the system can't find the reference to the page and field.

On the front end, since you're using the Grouped Attachment, you'll need to loop through the object and output the items. Here's a reference to a post I did on this a few years ago, but using Portal Engine vs. MVC. The concept is the same, but the language will be different. Here's some documentation on how to handle displaying this in MVC.

0 votesVote for this answer Mark as a Correct answer

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