Try our conversational search powered by Generative AI!

Episerver Forms File Upload - control of file destination

Vote:
 

Happy New Year!

Is anyone aware of a way to take control of where file is saved when uploaded via File Upload element of Episerver Form?
Precisely, am looking for a way to set another destination (folder) where images land.

#200140
Jan 03, 2019 11:16
Vote:
 

Hi Milos,

Had a quick look into this and if you want to change the destination of image uploads for all forms (which is what it sounds like) you could create a custom DataSubmissionService (EPiServer.Forms.Core.Internal) implementation.

Really simple example would be:

[ServiceConfiguration(typeof(DataSubmissionService), Lifecycle = ServiceInstanceScope.Singleton)]
public class CustomDataSubmissionService : DataSubmissionService
{
    protected override FileSaveItem StorePostedFile(long postId, HttpPostedFileBase postedFile,
        ContentReference folderLink)
    {
        var images = new [] { ".gif", ".jpg", ".png" };

        var extension = Path.GetExtension(postedFile.FileName);

        if (!images.Contains(extension, StringComparer.OrdinalIgnoreCase))
        {
            // If the extension is not an image save to the default folder
            return base.StorePostedFile(postId, postedFile, folderLink);
        }

        // Save images to the start page asset folder
        var startPageAssetFolder = _contentAssetHelper.Service.GetOrCreateAssetFolder(ContentReference.StartPage);
        return base.StorePostedFile(postId, postedFile, startPageAssetFolder.ContentLink);
    }
}

and the IConfigurableModule to replace the default implementation:

[InitializableModule]
[ModuleDependency(typeof(Web.InitializationModule))]
public class CustomDataSubmissionServiceInitialization : IConfigurableModule
{
    public void ConfigureContainer(ServiceConfigurationContext context)
    {
        context.Services.AddSingleton<DataSubmissionService, CustomDataSubmissionService>();
    }

    public void Initialize(InitializationEngine context) {}

    public void Uninitialize(InitializationEngine context) {}
}

That seems to be the easiest way to do it, unfortunately also touches a class in an internal namespace.

I see that when the default implementation creates the folder it also removes read access for the Every One role (see the GetOrCreateFolderForStoringFiles method), so you may want to consider that.

#200156
Jan 03, 2019 18:39
Vote:
 

Hi, thanks, I will give it a try!

#200170
Jan 04, 2019 8:53
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.