Get uploaded files from BizFormItem

Carson Wong asked on July 25, 2016 19:46

Hi,

I have a custom handler for form submission to handle custom submission to a web service, but I have no idea how to get all the uploaded files from the form BizFormItem field.

Recent Answers


Laura Frese answered on July 26, 2016 00:38

Its a little old but... This may help you

string fileNameString = "605b0932-660c-43cf-b2e0-b5ccf519f75f.png/originalFileName.png";

BasicForm bf = new BasicForm();

string fileName = bf.GetGuidFileName(fileNameString);
string filePath = FormHelper.GetBizFormFilesFolderPath(CMSContext.CurrentSiteName) + fileName;

if (File.Exists(filePath))
{
FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);

// Add attachment
Attachment attachment = new Attachment(stream, bf.GetFileNameForUploader(fileNameString));
}
0 votesVote for this answer Mark as a Correct answer

Carson Wong answered on July 26, 2016 17:48

How can I obtain the file name from the form?

0 votesVote for this answer Mark as a Correct answer

Laura Frese answered on July 27, 2016 00:25

You should be able to pull the file name or a GUID from the form data

0 votesVote for this answer Mark as a Correct answer

Francis Poirier answered on July 27, 2016 17:10

We are on Kentico 9, bz.GetGuidFileName() doesn't exist anymore neither does FormHelper.GetBizFormFilesFolderPath(). Is there any example how to read an uploaded file from a BizForm in Kentico 9 ?

0 votesVote for this answer Mark as a Correct answer

Laura Frese answered on July 27, 2016 17:39

Looks like you need to use CMS.OnlineForms now See the OnlineForms API reference here You might be able to use something like BizFormItem.GetExternalPath

There are some API examples here that might help

0 votesVote for this answer Mark as a Correct answer

Francis Poirier answered on July 27, 2016 22:57

BizFormItem.GetExternalPath is a protected method of the BizFormItem class. Is there any other way ?

0 votesVote for this answer Mark as a Correct answer

Laura Frese answered on July 28, 2016 04:03 (last edited on July 28, 2016 18:28)

using CMS.Base;
using CMS.OnlineForms;
using CMS.IO;

/// <summary>
/// Partial class that allows you to register custom handler methods and classes.
/// Adds the CustomFormHandlers attribute to the partial class.
/// </summary>
[CustomFormHandlers]
public partial class CMSModuleLoader
{
    /// <summary>
    /// Custom attribute class.
    /// </summary>
    private class CustomFormHandlers: CMSLoaderAttribute
    {
        /// <summary>
        /// Called automatically when the application starts
        /// </summary>
        public override void Init()
        {
            // Assigns a handler to the BizFormItemEvents.Insert.After event
            // This event occurs after the creation of every new form record
            BizFormItemEvents.Insert.After += FormItem_InsertAfterHandler;
        }

        /// <summary>
        /// Handles the form data when users create new records for the 'ContactUs' form
        /// </summary>
        private void FormItem_InsertAfterHandler(object sender, BizFormItemEventArgs e)
        {
            // Gets the form data object from the event handler parameter
            BizFormItem formDataItem = e.Item;

            // Checks that the form record was successfully created
            // Ensures that the custom actions only occur for records of the 'ContactUs' form
            // The values of form class names must be in lower case
            if (formDataItem != null && formDataItem.BizFormClassName == "bizform.contactus")
            {
                string firstNameFieldValue = formDataItem.GetStringValue("FirstName", "");
                string lastNameFieldValue = formDataItem.GetStringValue("LastName", "");


                // Perform any required logic with the form field values
                 FileInfo file_info = formDataItem.GetFileInfo("Attachment");
                 //You can get the data you need from the file_info including read the data stream in & send the stream to an external service.
                 string file_path_and_name = file_info.FullName;
            }
        }
    }
}
2 votesVote for this answer Mark as a Correct answer

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