File Upload for Sitecore Forms

Version 1.1 of the Sitecore Forms Extensions add the File Upload Control to your forms toolbox!

It features 2 implementations for storing your files (file system or azure blob storage) and is easily extensible to add your own storageprovider.

The control also comes with 2 validators. One for the content type of the files and one for the maximum file size.

Storage Providers

After installation of the module (just a sitecore package to install), you will have to configure a FileUploadStorageProvider.

This storage provider will take the uploaded file and store it somewhere. Currently, we have to storage providers out of the box.

File System

<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"  xmlns:role="http://www.sitecore.net/xmlconfig/role/">
  <sitecore>
    <formExtensions>
      <fileUploadStorageProvider type="Feature.FormsExtensions.Business.FileUpload.FileSystemFileUploadStorageProvider, Feature.FormsExtensions">
        <rootStoragePath>c:\temp\</rootStoragePath>
        <fileDownloadUrlBase>https://myfile.com/{0}</fileDownloadUrlBase>
      </fileUploadStorageProvider>
    </formExtensions>
  </sitecore>
</configuration>

For this provider, you have to configure 2 parameters:

  • rootStoragePath: the path on the local file system to store the file
  • fileDownloadUrlBase: this is the url to download the uploaded file afterwards, it should contain {0}, as the {0} will get replaced with the uploaded filename

Files are no stored under the original filename but they receive a random guid + the original extension.

Note that the module does not provide a method to download the files. It only constructs a download URL.

Beware that on load-balanced solutions, you will need to make sure that the storagePath is shared over the servers.

Azure Blob Storage

When using Azure, this is really my personal favourite option. The disk space is cheap, it is redundant, you don’t need to worry about scaled solutions, and the download functionality comes with the system.

<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"  xmlns:role="http://www.sitecore.net/xmlconfig/role/">
  <sitecore>
    <formExtensions>      
      <fileUploadStorageProvider type="Feature.FormsExtensions.Business.FileUpload.AzureBlobStorageFileUploadStorageProvider, Feature.FormsExtensions">
        <connectionString></connectionString>
        <blobContainer></blobContainer>
      </fileUploadStorageProvider>
    </formExtensions>
  </sitecore>
</configuration>

This provider has 2 parameters: connectionString and blobContainer.

To setup, create a storage account in Microsoft Azure.

Next create a storage container

Choose public access level blob. The name needs to be entered in the blobContainer parameter of the configuration.

The connectionstring can be found on the storage account under Access keys. Copy past the connectionstring under key1 or key2.

That’s it, no further config is required.

 

Custom Storage Provider

If you require the file to be written elsewhere, you can create your own storage provider.

For this, create a new class and extend from the IFileUploadStorageProvider interface.

    public interface IFileUploadStorageProvider
    {
        IStoredFile StoreFile(HttpPostedFileBase fileBase);
    }

The interface receives a HttpPostedFileBase param containing, the filestream, original filename, content type, etc..

The storefile method should return an implementation of IStoredFile

public interface IStoredFile
    {
        string Url { get; set; }
        string OriginalFileName { get; set; }
        string ContentType { get; set; }
        int ContentLength { get; set; }
    }

 

Validation

The new form upload control also supports 2 new validators: content-type and filesize validator.

Content Type

If you which to restrict the file upload to certain file types. You can enable the ContentType Validator.

When enabled, you need to fill in the Allowed Content Types field and add the content types you which to allow. Add multiple content types on seperate rows.

The validation is done client and server side.

File Size Validator

With the file size validator you can limit the maximum allowed file size of the uploads.

The value is in bytes and is validated client and server side.

Send Email Action

This control is also integrated with the send email to fixed address action of this module. The result will contain the url the $formFields$ and $form_fieldname$ tokens.

Save to database

In the database, a json serialised version of the IStoredFile interface is stored.

This contains the URL, original filename, content type and filesize.

7 Responses to “File Upload for Sitecore Forms”

Author's gravatar

Is it possible to use this component for Multi file uploads?

    Author's gravatar

    Hi Wilko!
    You can add multiple instances of this component to a single form.
    The component in itself on the other hand is driven by html’s standard and only supports one file.

Author's gravatar

I have installed 1.5.1 package for Upload functionality. But i see that text box fields are disappeared in WEB. I can see those in Experience editor.

How can i revert back the changes?

Author's gravatar

Hello Bart,

Is it possible to use CDN to send email and upload file in Azure blob, means it will have different URL while I am sending email.

    Author's gravatar

    Thanks for the suggestion. Currently (1.7), there is no option in the configuration.

    However, you can look at the code of FileSystemFileUploadStorageProvider, copy paste from that one to create your own implementation. And configure the module to use your custom storage provider.

Author's gravatar

Any way to give CDN url to download the file in email (instead of blob url)?

Leave a Reply