asp.net mvc 3 razor file upload

后端 未结 3 1380
温柔的废话
温柔的废话 2020-12-30 17:14

Which is best way to upload single file with asp.net mvc3 razor and validate with jquery.

I only need to user upload jpg, png less then 5 mb.

Thanks

3条回答
  •  长情又很酷
    2020-12-30 17:46

    You will need to validate with javascript, here is a sample

    function onSelect(e) {
        if (e.files[0].size > 256000) {
            alert('The file size is too large for upload');
            e.preventDefault();
            return false;
        }
        // Array with information about the uploaded files
        var files = e.files;
        var ext = $('#logo').val().split('.').pop().toLowerCase();
        if ($.inArray(ext, ['gif', 'jpeg', 'jpg', 'png', 'tif', 'pdf']) == -1) {
            alert('This type of file is restricted from being uploaded due to security reasons');
            e.preventDefault();
            return false;
        } 
        return true;
    }
    

    this says the file must be no greater than 256K and allows only gif, jpg, jpeg, tif, png and pdf. Just change 256000 to 5000000 and your specific file type

    I'm using this in MVC 3 in a razor view with the Telerik upload control. You can use with a standard upload input as well, just fire this event on selection or before committing

提交回复
热议问题