Downloading files uploaded to the cms

Luke Taylor asked on March 18, 2020 12:01

I want to be able to upload a file to the cms, to a field on a page. Then from the actually site page, have a link to download said file

Recent Answers


Arjan van Hugten answered on March 18, 2020 14:54 (last edited on March 18, 2020 15:09)

Hi,

I see that you also posted the question here on devnet. Below the same answer that I posted on stackoverflow.

I think that a nice way to do this is with kentico page attachments. On the page in the content tree you can go to attachments. There you can add for example a pdf file. This can then be retrieved on the backend and then you can create a link on the view with it. Also see this link about displaying the page attachments. You can also take a look at attaching file groups to pages.

Example:

Controller:

viewModel.FileUrl = treeNode.AllAttachments?.FirstOrDefault()?.GetPath() ?? string.Empty;

View:

<a href="@Model.FileUrl" target="_blank">Download file</a>
0 votesVote for this answer Mark as a Correct answer

Luke Taylor answered on March 18, 2020 15:35

Is there a way to seperate the attachments into groups of sorts? Like if my page has a table with two columns, one for pictures of me, one for pictures of dogs? So i can do some sort of attachments.Where(in dog)

0 votesVote for this answer Mark as a Correct answer

Arjan van Hugten answered on March 18, 2020 15:46

Yes you can add fields to the page type, one for the pictures of your self and one for the pictures of dogs. The field data type needs to be 'Attachments'.

Then you can access them from the page with the field names.

var dogPictures = page.Fields.DogAttachments;
var picturesOfMyself = page.Fields.PicturesOfMyselfAttachments;
0 votesVote for this answer Mark as a Correct answer

Arjan van Hugten answered on March 20, 2020 09:54

Hi,

Haven't heard from you for a while now. Was this the answer that you were looking for?

0 votesVote for this answer Mark as a Correct answer

Luke Taylor answered on March 20, 2020 10:21

Yes, sorry i thought i flagged the answer as correct

0 votesVote for this answer Mark as a Correct answer

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