0

By default files are getting uploaded to the media library folder. How do I make WFFM upload the files to a different path by default? This is in MVC WFFM forms.

1 Answer 1

4

One of the properties on the File upload field type is Upload to. Select the elipsis then set the correct folder in the media library from the dialog.

Don't forget to publish the form.

WFFM Upload to Media Library


If you want to always default the value a different folder then you will need to override the field and the attributes of the UploadTo property.

Create a class with the following, which will be used in the Form Designer interface and WebForms implementations:

using System.ComponentModel;
using Sitecore.Form.Core.Attributes;
using Sitecore.Form.Core.Visual;

namespace MyProject.CMS.Custom.WFFM.Fields
{
    public class FileUpload : Sitecore.Form.Web.UI.Controls.UploadFile
    {
        private string uploadDir = "/sitecore/media library/Uploads";

        [VisualCategory("Upload")]
        [VisualProperty("Upload To:", 0)]
        [VisualFieldType(typeof(SelectDirectoryField))]
        [DefaultValue("/sitecore/media library/Uploads")]
        public new string UploadTo
        {
            get
            {
                return this.uploadDir;
            }
            set
            {
                this.uploadDir = value;
            }
        }
    }
}

Create another class for MVC implementation:

using System.ComponentModel;

namespace MyProject.CMS.Custom.WFFM.Fields
{
    public class FileUploadField : Sitecore.Forms.Mvc.ViewModels.Fields.FileUploadField
    {
        [DefaultValue("/sitecore/media library/Uploads")]
        public string UploadTo { get; set; }

        public FileUploadField()
        {
            if (!string.IsNullOrEmpty(this.UploadTo))
                return;
            this.UploadTo = "/sitecore/media library/Uploads";
        }
    }
}

Set the DefaultValue and other properties to whatever your default folder should be.

Next update the File Upload field located at /sitecore/system/Modules/Web Forms for Marketers/Settings/Field Types/Simple Types/File Upload with the details of your assembly:

  • Assembly: MyProject.CMS.Custom
  • Class: MyProject.CMS.Custom.WFFM.Fields.FileUpload
  • MVC Type: MyProject.CMS.Custom.WFFM.Fields.FileUploadField, MyProject.CMS.Custom

NOTE: It is recommended that instead of directly updating the above field you instead make a copy and use that copy throughout your forms instead. The downside if you'll have 2 file upload type fields available, but you also risk potential issues if an upgrade modifies this field type.

4
  • Thanks jammy for your reply. By default the upload property value appears to be media library. We have a requirement to have the default value as different folder, say for eg site/global/some folder. I tried overriding uploadto property of sitecore.mvc.forms.viewmodel.fileupload , but that appears to be executing after closing the form designer
    – aish
    Commented Aug 3, 2017 at 5:37
  • @aish: Updated answer with how to override the default values.
    – jammykam
    Commented Aug 3, 2017 at 23:53
  • I have implemented the way as suggested above. The FileUpload is class called only for the first time. The Fileupload class is not being called every time we add the FileUpload control in the form desinger. Hence unable to load the default value for Uploadto Property based on country site. Can you suggest on this.
    – aish
    Commented Aug 17, 2017 at 13:44
  • I don't know off the top of my head and without digging into it. I would suggest you open a new question with details of what you are doing. If the above answered your originla question then please mark it as answered.
    – jammykam
    Commented Aug 17, 2017 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.