# Manage file storage for forms

Abstract

Learn how to add new file storage and create a custom FileStorageProvider class

Sitecore Forms contains a file upload element that you can use to allow a visitor to attach a single file or multiple files to your web form. For example, a resume, an avatar, or a profile picture. This topic describes how to manage the storage of these uploaded files.

By default, all forms data is stored in the Experience Forms database. In the Sitecore.ExperienceForms.config file, you can view the default setting by checking the following connection string: Sitecore.ExperienceForms.FileStorageConnectionStringName.

For scalability reasons, you might want to separate the storage for general forms data coming from the form fields and the storage for files uploaded by visitors.

To connect Sitecore Forms file storage to a different database:

1. Open the Sitecore.ExperienceForms.config file and add a setting using the following connection string: Sitecore.ExperienceForms.FileStorageConnectionStringName

For example:

<setting name="Sitecore.ExperienceForms.FileStorageConnectionStringName" value=fileStorage" />
2. Open the ConnectionStrings.config file and add the connection string that you just created. For example:

<add name="fileStorage" connectionString="Data Source=.;Initial Catalog=sc930xp0_FileStorage;User ID=formsuser;Password=sitecore" />

You can develop a different FileStorageProviderclass by implementing the IFileStorageProvider interface.

To create a custom FileStorageProvider:

1. Create your own FileStorageProvider class. To extend the IFileStorageProvider, you must add a reference to the Sitecore.ExperienceForms.Data assembly.


public interface IFileStorageProvider

{

Guid StoreFile(Stream file, string fileName);

StoredFile GetFile(Guid fileId);

void DeleteFiles(IEnumerable<Guid> fileIds);

void CommitFiles(IEnumerable<Guid> fileIds);

void Cleanup(TimeSpan timeSpan);

}



The following table describes the commands:

Command

Description

StoreFile

Stores a new file into the storage. Returns the GUID of the stored file.

GetFile

Retrieves the file information and file content in a StoredFile object. The StoredFile class contains the following:

public class StoredFile
{
public StoredFileInfo FileInfo { get; set; }
public Stream File { get; set; }
}


DeleteFIles

Deletes files from the storage.

CommitFIles

Commits the files inside the storage. Files that are committed are not removed during cleanup.

2. To register the customized file provider, open the Sitecore.ExperienceForms.config file and search for Sitecore.ExperienceForms.Data.IFileStorageProvider, Sitecore.ExperienceForms.

3. Replace the implementation type with your FileStorageProvider class name and assembly name.

<register serviceType="Sitecore.ExperienceForms.Data.IFileStorageProvider, Sitecore.ExperienceForms" implementationType="SItecore.ExperienceForms.Data.SqlServer.SqlFilestorageProvider, Sitecore.ExperienceForms.Data.SqlServer" lifetime="Transient" />

Sitecore Forms automatically stores uploaded files when a visitor navigates between form pages, even if the form is not submitted. How long an uncommitted file is stored is determined by the FileGracePeriod setting (the default is 4 hours).

To define how long an uncommitted file is stored, change the FileGracePeriod setting:

• Open the Sitecore.ExperienceForms.config file and go Sitecore.ExperienceForms.FileGracePeriod.

<setting name="Sitecore.ExperienceForms.FileGracePeriod" value="00:04:00:00" />

The cleanup mechanism cleans uncommitted files. By default, the cleanup task runs once every 24 hours.

To control commands and schedules for cleaning the Forms database:

• In the Master database, go to sitecore/System/Tasks/

The FileStorageCleanup command includes the following fields:

Field

Description

Type

The full name and the assembly name of your cleanup class. For example: Sitecore.ExperienceForms.Tasks.FileStorageCleanup,Sitecore.ExperienceForms

Method

The name of the method. For example: Execute

The TaskSchedule item defines when a task runs. The FileStorageCleanup schedule item defines how a task runs and includes the following fields:

Field

Description

Command

The name of the command. For example: Commands/Forms/FileStorageCleanup.

Items

Specify the list of items to be passed to the command method execution.

Schedule

Specify the schedule of the task. By default, the cleanup runs every 24 hours: 20190101|99990101|127|24:00:00

Last run

The last run time of the task. By default, Sitecore automatically updates the Last Run field after the cleanup task is complete.

Async

Select to run the task asynchronously.

Auto remove

Select to automatically remove the scheduled item after invoking the task.