Question

Hello every one i am trying to create a "tell a friend like form" in WFFM (sitecore) but i also add an functionality to attach a file in the form, this is working fine. Now i want forbid a user to attach large file more then 1 MB for that i create a new class

public class LimiteFileSize 
{
  public void Process(FormUploadFileArgs args)
  {
    int size = 1049000;

    if (args.File.Data.Length > size)
    {
      Sitecore.Diagnostics.Log.Error(string.Format("User {0} tried to upload a file larger than 10 Mb. The file name is {1}",
                                     Sitecore.Context.User.Name,
                                     args.File.FileName), this);
      args.AbortPipeline();
    }
  }
}

and register it in forms.config

<formUploadFile>
  <processor type="Sitecore.Form.Core.Pipelines.FormUploadFile.ResolveFolder, Sitecore.Forms.Core"/>
  <processor type="Sitecore.Form.Core.Pipelines.FormUploadFile.Save, Sitecore.Forms.Core"/>
  <processor type="scwffm2.Helper.LimiteFileSize, scwffm2.Helper"/>  
</formUploadFile>

Now if i don't comment or remove the ( this is default save action in WFFM) `

<processor type="Sitecore.Form.Core.Pipelines.FormUploadFile.Save, Sitecore.Forms.Core"/>

` large file will upload using default action and if the above line is commented then it will not save any file in database even large or small. The problem is that porcess in the LimiteFileSize is working fine but it only check the file size it does not save file if file size is less then 1MB. should i take an else condition for the file size less then 1 MB. ??

Was it helpful?

Solution

I think you should put your processor above Sitecore's processor, as they run in order. In your case it should be:

<formUploadFile>
    <processor type="scwffm2.Helper.LimiteFileSize, scwffm2.Helper"/>  
    <processor type="Sitecore.Form.Core.Pipelines.FormUploadFile.ResolveFolder, Sitecore.Forms.Core"/>
    <processor type="Sitecore.Form.Core.Pipelines.FormUploadFile.Save, Sitecore.Forms.Core"/>
</formUploadFile>

Another option would be to check the filesize using jQuery, making sure you can't even upload the file in the first place:

$.validator.addMethod('filesize', function(value, element, param) {
    // param = size (en bytes) 
    // element = element to validate (<input>)
    // value = value of the element (file name)
    return this.optional(element) || (element.files[0].size <= param) 
});

$('#inputid').validate({
    rules: { input: { required: true, filesize: 1048576  }},
    messages: { input: "File must be less than 1MB" }
});

Please note that I haven't tested this code, so it might need some tweaking

OTHER TIPS

As Trayek solution forces the file size for every form on the website I'd like to suggest an alternate option.

Use Sitecore's FormCustomValidator to validate the file are less than a certain size, they are made for these very scenarios. Using the following method you can define a file limit for each upload field added to a Form.

public class FileSizeValiadtor : FormCustomValidator
{
    protected override bool EvaluateIsValid()
    {
        bool isValid = false;

        var fileUpload = this.FindControl(base.ControlToValidate) as FileUpload;
        if (fileUpload != null && fileUpload.HasFile)
        {
            int fileSizeLimitinBytes = GetFileSizeLimit();

            int sizeInBytes = fileUpload.PostedFile.ContentLength;
            isValid = (sizeInBytes <= fileSizeLimitinBytes);
        }

        return isValid;
    }

Then you will need add a BaseValidator Item in the WFFM Validation Folder, usually this path; /sitecore/system/Modules/Web Forms for Marketers/Settings/Validation. Add the assembly and class of this new validator to the item. Finally adding your BaseValiator Item in the Validation field of the FileUpload Field.

Then its a matter of getting the FileSizeLimit, you could get this from a Field on a particular Sitecore item or even extend the FileUpload Field to let the user de to give a textbox to the user and get the value from there.

        [VisualFieldType(typeof(EditField))]
        [VisualProperty("Max file size limit (MB) :", 5)]
        public string FileSizeLimit
        {
            get
            {
                return this._fileSizeLimit.ToString();
            }
            set
            {
                this._fileSizeLimit = int.Parse(value);
            }
        }

See here for a full example

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top