Can anyone shed some light on why the following regex fails when implemented? I am trying to limit the file upload field to only accept jpg/JPG. I'm choosing "...or enter custom validation" and entering:
([^\s]+(\.(?i)(jpg))$)
Is there a particular format the expression needs to be entered in?
Where do you want to add this validation? I make a custom validation control inherited from normal fileupload. The control can be used in a Form.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using Umbraco.Forms.Core;
using System.Collections.Specialized;
using System.Configuration;
namespace YourNameSpace
{
public class CustomFileUpload : Umbraco.Forms.Core.Providers.FieldTypes.FileUpload
{
private int _maxFileSize = 5242880;
private string _ContentType = "application/pdf";
public CustomFileUpload()
{
Id = new Guid("d8d89001-57ce-4b52-9005-c0c84f6651cb");
Name = "Custom file upload";
Description = "Limita a subir solo PDF o JPG y de hasta 1 MB";
Icon = "icon-cloud-upload ";
DataType = FieldDataType.String;
FieldTypeViewName = "FieldType.CustomFileUpload.cshtml";
}
NameValueCollection coleccion = (NameValueCollection)ConfigurationManager.GetSection("customfileUploadSettings/tipoCustomfileUploadSettings");
public override IEnumerable<string> ValidateField(Form form, Field field, IEnumerable<object> postedValues, HttpContextBase context)
{
IList<HttpPostedFileBase> files = context.Request.Files.GetMultiple(field.Id.ToString());
foreach (HttpPostedFileBase file in files)
{
int cantidad = 0;
int.TryParse(coleccion.Get("MaxFileSize"), out cantidad);
if (file.ContentLength > cantidad)
{
return new[]
{coleccion.Get("MaxFileSizeMsg")};
}
else
{
if (file.ContentType != coleccion.Get("ContentTypePDF"))
{
if (file.ContentType == coleccion.Get("ContentTypeIMG"))
{
string ext = Path.GetExtension(file.FileName).ToUpper();
if (ext != coleccion.Get("AllowExtension"))
{
return new[]
{ coleccion.Get("ContentTypeErroMsg")};
}
}
else
{
return new[]
{ coleccion.Get("ContentTypeErroMsg")};
}
}
}
if (file.ContentLength == 0)
{ return Enumerable.Empty<string>(); }
}
return Enumerable.Empty<string>();
}
}
I wanted to add the simple validation as shown below.
Whilst your method looks good, I need it to work with the Image Cropper datatype. Sorry, I should have been more specific, I didn't mean the file upload datatype.
Custom validation jpg only
Can anyone shed some light on why the following regex fails when implemented? I am trying to limit the file upload field to only accept jpg/JPG. I'm choosing "...or enter custom validation" and entering:
Is there a particular format the expression needs to be entered in?
Hi Joel.
Where do you want to add this validation? I make a custom validation control inherited from normal fileupload. The control can be used in a Form.
{
}
Gracias Martin.
I wanted to add the simple validation as shown below.
Whilst your method looks good, I need it to work with the Image Cropper datatype. Sorry, I should have been more specific, I didn't mean the file upload datatype.
is working on a reply...