Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • sam how 14 posts 94 karma points
    Oct 12, 2023 @ 06:20
    sam how
    0

    Custom Fields File Upload (Umbraco Forms)

    Hello,

    How can I limit File Size in Umbraco Forms File Upload Field?

  • sam how 14 posts 94 karma points
    Oct 16, 2023 @ 09:20
    sam how
    0

    I was able to limit the file size using Custom Fields. I followed the instruction in the documentation and I was able to limit using javascript. My Problem right now is when I change the input type to file from text I am getting a value of null. I am new to .Net so I'm no really sure what's wrong with my code. Please help.

    FieldType.CustomField.cshtml

    @model Umbraco.Forms.Web.Models.FieldViewModel
    
    <input type="file" name="@Model.Name" id="@Model.Id" class="upload-resume" accept=".pdf, .doc, .docx"
    @{
        if (string.IsNullOrEmpty(Model.PlaceholderText) == false)
        {
            <text> placeholder="@Model.PlaceholderText" </text>
        }
    }
    @{
        if (Model.Mandatory || Model.Validate)
        {
            <text> data-val="true" </text>
        }
    }
    @{
        if (Model.Mandatory)
        {
            <text> data-val-required="@Model.RequiredErrorMessage" </text>
        }
    }
     />
    
    <script>
        $(document).ready(function () {
            $('.upload-resume').on('change', function () {
                var input = this;
    
                if (input.files && input.files.length > 0) {
    
                    var fileSize = input.files[0].size; // in bytes
                    console.log(fileSize);
    
    
                    var maxSize = 1024 * 1024; // 1MB
    
    
                    if (fileSize > maxSize) {
                        alert("File size exceeds the allowed limit. Please choose a smaller file.");
                        $(input).val('');
                    }
                }
            });
        });
    </script>
    

    CustomField.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Microsoft.AspNetCore.Http;
    using Umbraco.Forms.Core.Enums;
    using Umbraco.Forms.Core.Models;
    using Umbraco.Forms.Core.Services;
    
    namespace MyGlobal.FormsExtensions.Fields
    {
        public class CustomField : Umbraco.Forms.Core.FieldType
        {
            public CustomField()
            {
                Id = new Guid("08b8057f-06c9-4ca5-8a42-fd1fc2a46eff"); 
                Name = "Custom File Upload";
                Description = "Renders an upload field.";
                Icon = "icon-cloud-upload";
                DataType = FieldDataType.String;
                FieldTypeViewName = "FieldType.CustomField.cshtml";
            }
    
            // You can do custom validation in here which will occur when the form is submitted.
            // Any strings returned will cause the submission to be considered invalid.
            // Returning an empty collection of strings will indicate that it's valid to proceed.
    
            public override IEnumerable<string> ValidateField(Form form, Field field, IEnumerable<object> postedValues, HttpContext context, IPlaceholderParsingService placeholderParsingService, IFieldTypeStorage fieldTypeStorage)
            {
                var returnStrings = new List<string>();
                var file = postedValues.FirstOrDefault() as IFormFile;
    
                if (file == null)
                {
                    returnStrings.Add("Please upload a file.");
                }
                else
                {
                    // Add additional file-specific validation if needed
                    // For example: Check file size, file type, etc.
                    // Example: if (file.Length > maxSize) returnStrings.Add("File size exceeds the limit.");
                }
    
                // Validate against the default method
                return base.ValidateField(form, field, postedValues, context, placeholderParsingService, fieldTypeStorage);
            }
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft