This is a migrated thread and some comments may be shown as answers.

limit upload image file dimension

1 Answer 65 Views
FileExplorer
This is a migrated thread and some comments may be shown as answers.
sf
Top achievements
Rank 1
sf asked on 29 Nov 2012, 07:33 AM
I restricted my fileexplorer to only accept images using SearchPattern property.
Is it possible that we can also control the dimension of the image file that gets uploaded? e.g. only allow people to upload image files with dimension of 500x600.

thanks in advance

1 Answer, 1 is accepted

Sort by
0
Vessy
Telerik team
answered on 04 Dec 2012, 12:07 AM
Hi,

Currently the desired functionality is not available out of the box but you could achieve it by subclassing the existing FileSystemContentProvider and overriding the StoreFile() method. In order to take the dimensions of the uploaded images, you could create an instance of the EditableImage class from the uploaded file's memory stream and access them in a similar way:
public partial class FileExplorer : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        RadFileExplorer1.Configuration.ContentProviderTypeName = typeof(CustomFileSystemProvider).AssemblyQualifiedName;
     
    }
}
 
public class CustomFileSystemProvider : Telerik.Web.UI.Widgets.FileSystemContentProvider
{
    public CustomFileSystemProvider(HttpContext context, string[] searchPatterns, string[] viewPaths, string[] uploadPaths, string[] deletePaths, string selectedUrl, string selectedItemTag)
        : base(context, searchPatterns, viewPaths, uploadPaths, deletePaths, selectedUrl, selectedItemTag)
    {
    }
    public override string StoreFile(UploadedFile file, string path, string name, params string[] arguments)
    {
        var maxHeight = 500;
        var maxWidth = 600;
        string targetPath = Path.Combine(path, name);
        var physicalPath = MapPath(targetPath);
 
        var editableImage = new EditableImage(file.InputStream);
        var height = editableImage.Height;
        var width = editableImage.Width;
 
        if (height > maxHeight || width > maxWidth)
        {
            return string.Empty;
        }
        //existing file will be overwritten!
        file.SaveAs(physicalPath);
        return targetPath;
    }

Regards,
Vesi
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
FileExplorer
Asked by
sf
Top achievements
Rank 1
Answers by
Vessy
Telerik team
Share this question
or