Manage file storage for forms
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
.
Add new file storage using a connection string
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:
-
Open the
Sitecore.ExperienceForms.config
file and add a setting using the following connection string:Sitecore.ExperienceForms.FileStorageConnectionStringName
For example:
RequestResponse<setting name="Sitecore.ExperienceForms.FileStorageConnectionStringName" value=fileStorage" />
-
Open the
ConnectionStrings.config
file and add the connection string that you just created. For example:RequestResponse<add name="fileStorage" connectionString="Data Source=.;Initial Catalog=sc930xp0_FileStorage;User ID=formsuser;Password=sitecore" />
Create a custom FileStorageProvider class
You can develop a different FileStorageProvider
class by implementing the IFileStorageProvider
interface.
To create a custom FileStorageProvider
:
-
Create your own
FileStorageProvider
class. To extend theIFileStorageProvider
, you must add a reference to theSitecore.ExperienceForms.Data
assembly.RequestResponsepublic 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. TheStoredFile
class contains the following:RequestResponsepublic 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.
-
To register the customized file provider, open the
Sitecore.ExperienceForms.config
file and search forSitecore.ExperienceForms.Data.IFileStorageProvider, Sitecore.ExperienceForms
. -
Replace the implementation type with your
FileStorageProvider
class name and assembly name.RequestResponse<register serviceType="Sitecore.ExperienceForms.Data.IFileStorageProvider, Sitecore.ExperienceForms" implementationType="SItecore.ExperienceForms.Data.SqlServer.SqlFilestorageProvider, Sitecore.ExperienceForms.Data.SqlServer" lifetime="Transient" />
Plan a cleanup task
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 goSitecore.ExperienceForms.FileGracePeriod
.RequestResponse<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. TheFileStorageCleanup
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.