asp.net MVC 3剃须刀文件上传 [英] asp.net mvc 3 razor file upload

查看:93
本文介绍了asp.net MVC 3剃须刀文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是上传与asp.net MVC3剃刀单个文件和JQuery验证最佳方式。

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

我只需要用户上传JPG,PNG小于5 MB。

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

感谢

推荐答案

您需要用JavaScript来验证,这里是一个样本

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;
}

这表示,文件大小不得超过256K更大,只允许GIF,JPG,JPEG,TIF,PNG和PDF。只要改变为256000 5000000,您的特定文件类型

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

我在MVC 3与Telerik的上传控件剃刀视图用这个。你可以用一个标准上传输入使用,以及,刚刚火上选择或之前提交此事件

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

这篇关于asp.net MVC 3剃须刀文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆