How to Get a File Attached to a Kentico Form?

kentico guy asked on April 7, 2022 23:37

Kentico 12.0.92 portal engine: I'm trying to get an attachment stored in the database and attach it to an email. The file is attached to a standard kentico form using the upload file control. I see that the column "UploadFile" is populated like this after my form gets submitted but if I try to do a GetFieldValue("UploadFile") it comes back null.

In the database the UploadFile fields are formatted as such:

66af607c-2fbc-4d8c-9880-3bf50b2959e8.png/db.png

(that's an example).

It seems like there are old answers on how to do this from around 2016 and older but none of them worked for me. I am trying to get the file specifically as a System.IO.Stream because I'm trying to attach it to an email. But really if anyone has any ideas on how to get the FileInfo object I can probably figure out how to convert it to a Stream. It's been a major challenge just to do something as simple as get an attachment. I can't believe it's this difficult; you'd think that this would be easy since what's the point of attaching something if you can't get it from the database?

Correct Answer

kentico guy answered on April 8, 2022 22:51

I ended up using a BizFormItemEvents.Insert.After event handler rather than the OnAfterSave event. I think the problem was that the attachment hadn't been fully uploaded until after insert

    protected override void OnLoad(EventArgs e)
{
    BizFormItemEvents.Insert.After += FormItem_InsertAfterHandler;
    base.OnLoad(e);
}

private void FormItem_InsertAfterHandler(object sender, BizFormItemEventArgs e)
{ 
            BizFormItem formDataItem = e.Item;
            var myAttachment = (string)formDataItem.GetValue("MyAttachmentFieldName");
            // this part needs to be split because the name in DB won't match filesystem
            var fullFilePath = $"D:/Web/MyAppFolder/CMS/MyMediaFolder/BizFormFiles/{myAttachment.Split('/')[0]}";
            CMS.EmailEngine.EmailMessage em = new CMS.EmailEngine.EmailMessage();
            em.EmailFormat = CMS.EmailEngine.EmailFormatEnum.Html;
            var attachedFile = new System.Net.Mail.Attachment(fullFilePath);
            // then need to set the file name or else it'll be a path
            attachedFile.Name = $"{attachment.Split('/')[attachment.Split('/').Length - 1]}";
            em.Attachments.Add(attachedFile);
            // you also need to assign the other em.From, subject, body etc fields here but that's easy
            // then just send it
            CMS.EmailEngine.EmailSender.SendEmail(SiteContext.CurrentSiteName, em, true);
}

Then I found out that even if "save to database" is checked for form items they still end up in the file system @ CMS/MyMediaFolderHere/BizFormFiles/~ so I had to add a setting to get the full filepath. Then the attachment can be created from the filepath and added to an email per instructions that can be found online System.Net.Mail.Attachment class

0 votesVote for this answer Unmark Correct answer

Recent Answers


Juraj Ondrus answered on April 8, 2022 06:00

If you want to get a page attachment, you can use the API example and then, the attachment object has the AttachmentBinary property which holds the binary data. Or, you can check the methods available in the AttachmentInfoProvider class. for example, the last overload of the GetAttachmentInfo method has the parameter telling whether or not to return the binary data.

0 votesVote for this answer Mark as a Correct answer

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