Get Attachments from CMS_Attachment using a link

Stefan Sturm asked on February 14, 2023 13:33

Hello

we plan to store uploaded images within the CMS_Attachment table. Therefore we use this piece of code:

var newAttachment = new AttachmentInfo()
{
    AttachmentGUID = existingGUID,
    AttachmentName = file.FileName,
    InputStream = file.InputStream,
    AttachmentExtension = System.IO.Path.GetExtension(file.FileName),
    AttachmentSize = file.ContentLength,
    AttachmentMimeType = file.ContentType,
    AttachmentSiteID = currentDocument.Site.SiteID
};
AttachmentInfoProvider.SetAttachmentInfo(newAttachment);

And this is working fine. The attachment is stored in the DB. We want to provide a link to download this attachment (MVC view). I tried to work with:

<a href="@Url.FormatUrl(AttachmentURLProvider.GetAttachmentUrl("[AttachmentGUID]", "image1"))" class="col-blue" download>Image</a>

But this is not working. How can I get a download link for the attachment?

Kind regards Stefan

Recent Answers


Not Applicable answered on February 15, 2023 11:46

Have you enabled Use permanent URLs? That's a prerequisite for this, see Displaying page attachments.

This should return something like ~/getattachment/image1/e80498af-fc24-418d-9e73-3387509dbcbf. It doesn't check whether the GUID is valid or not. So, you should also get a result for a non-existent GUID.

0 votesVote for this answer Mark as a Correct answer

Stefan Sturm answered on February 16, 2023 08:56

Hello Marcel,

I am getting a link as you have mentioned like ~/getattachment/image1/e80498af-fc24-418d-9e73-3387509dbcbf but when I try to download the attachment it always shows .html as the file type.

Permanent URL is enabled. Also, I am not using page attachments. I don't want to attach images to a specific page since this can cause problems when it comes to staging.

kind regards

0 votesVote for this answer Mark as a Correct answer

Not Applicable answered on February 16, 2023 09:59

0 votesVote for this answer Mark as a Correct answer

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