0

I am implementing an image upload option in my MVC application but I keep getting the following error: The type or namespace name 'FileSizeAttribute' could not be found.

As you can see from my code below I am implementing cusom attributes to determine the allowed file size and type.

public class UploadImageModel
    {
        [FileSize(10240)]
        [FileTypes("jpg,jpeg,png")]
        public HttpPostedFileBase File { get; set; }
    }

I define these attributes in my ProfileController.cs as you can see here.

public class FileSizeAttribute : ValidationAttribute
        {
            private readonly int _maxSize;

            public FileSizeAttribute(int maxSize)
            {
                _maxSize = maxSize;
            }

            public override bool IsValid(object value)
            {
                if (value == null) return true;

                return _maxSize > (value as HttpPostedFile).ContentLength;
            }
            public override string FormatErrorMessage(string name)
            {
                return string.Format("The image size should not exceed {0}", _maxSize);
            }
        }

        public class FileTypesAttribute : ValidateInputAttribute
        {
            private readonly List<string> _types;

            public FileTypesAttribute(string types)
            {
                _types = types.Split(',').ToList();
            }

            public override bool IsValid(object value)
            {
                if (value == null) return true;

                var fileExt = System.IO.Path.GetExtension((value as HttpPostedFile).FileName).Substring(1);
                return _types.Contains(fileExt, StringComparer.OrdinalIgnoreCase);
            }

            public override string FormatErrorMessage(string name)
            {
                return string.Format("Invalid file type. Only the following types {0} are supported.",
                    String.Join(", ", _types));
            }
        }
        [HttpPost]
        public ActionResult PhotoUpload(UploadImageModel imageModel)
        {
            string path = @"D:\Temp\";

            if (ModelState.IsValid)
            {
                if (imageModel != null && imageModel.File != null)
                    image.SaveAs(path + imageModel.File.FileName);

                return RedirectToAction("Profile");
            }

            return View();
        }

I should mention that I am adapting code from this article as I am new to MVC and just learning the ropes. That said, do I need to include a using clause in my header the references my Controller or is it a syntax issue that I am missing? Thanks in advance for the help.

3
  • Is the class in the same namespace as the model class? Otherwise, yes, you need to add a using directive. Commented Aug 13, 2014 at 3:20
  • @TiesonT. one is in the artisan.Models namespace and the other is in the artisan.Controllers namespace. Would this be a problem? If so what using directive should I use? Commented Aug 13, 2014 at 3:29
  • The attribute should really be in it's own file. What namespace you give it is up to you, but then you add that namespace to the model class. So, if your attribute was in artisan.Attributes, you would add a using artisan.Attributes; to the model class. Commented Aug 13, 2014 at 3:34

1 Answer 1

2

Let's assume you, at a minimum, moved those attributes out into a combined file, call it ExtensionAttributes.cs. Inside that file, you would have something like

namespace artisan.Attributes
{
    public class FileSizeAttribute : ValidationAttribute
    {
        private readonly int _maxSize;

        public FileSizeAttribute(int maxSize)
        {
            _maxSize = maxSize;
        }

        public override bool IsValid(object value)
        {
            if (value == null) return true;

            return _maxSize > (value as HttpPostedFile).ContentLength;
        }
        public override string FormatErrorMessage(string name)
        {
            return string.Format("The image size should not exceed {0}", _maxSize);
        }
    }

    public class FileTypesAttribute : ValidateInputAttribute
    {
        private readonly List<string> _types;

        public FileTypesAttribute(string types)
        {
            _types = types.Split(',').ToList();
        }

        public override bool IsValid(object value)
        {
            if (value == null) return true;

            var fileExt = System.IO.Path.GetExtension((value as HttpPostedFile).FileName).Substring(1);
            return _types.Contains(fileExt, StringComparer.OrdinalIgnoreCase);
        }

        public override string FormatErrorMessage(string name)
        {
            return string.Format("Invalid file type. Only the following types {0} are supported.",
                String.Join(", ", _types));
        }
    }
}

Then, to use the attribute on your model, you would do something like

using artisan.Attributes;

public class UploadImageModel
{
    [FileSize(10240)]
    [FileTypes("jpg,jpeg,png")]
    public HttpPostedFileBase File { get; set; }
}

Otherwise, as-is, you need to add a artisan.Controllers to your model class, but that (given the default project template) might cause some circular references.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.