English 中文(简体)
使用 Html.Telerik (). 用 MVC3 上载 () 校验
原标题:Validation using Html.Telerik().Upload() with MVC3

我正在寻找样本代码, 该代码将显示如何设定 Html. Telerik (). Upload () 函数的验证, 这样用户只能上传 .jpg 文件。 我想只支持 .jpg 文件 。

有谁有我可以利用的好例子或网站吗?

问题回答

不幸的是, 无法使用 < strong> Html. Telerik () 过滤扩展 。 上加() 是因为浏览器执行 < code@ lt; input type= " file" / & gt; 的方式 。

Source: http://www.telerik.com/community/forums/aspnet-ajax/upload/how-to-filter-out-unwanted-file-types-radupload.aspx

但用 html 和 手笔来做到这一点还有别的办法:

HTML HTML

<input name="fileToUpload" type="file" onchange="check_file()" >

手笔

function check_file(){
            str=document.getElementById( fileToUpload ).value.toUpperCase();
    suffix=".JPG";
    suffix2=".JPEG";
    if(!(str.indexOf(suffix, str.length - suffix.length) !== -1||
                   str.indexOf(suffix2, str.length - suffix2.length) !== -1)){
    alert( File type not allowed,
Allowed file: *.jpg,*.jpeg );
        document.getElementById( fileToUpload ).value=  ;
    }
}

资料来源:https://stackoverflow.com/a/6788623/1304064

请见以下代码。 此代码只接受 jpeg/ jpg, png 格式图像。 图像大小也限制在 100KB 。

@(Html.Telerik().Upload()
        .Name("EmployeeImageUpload")
        .Multiple(false)
        .Async(async => async
        .Save("SaveEmployeeImage", "Employee")
        .AutoUpload(false)
        )
        .ClientEvents(events => events
        .OnLoad("onLoad")
        .OnSelect("onSelect")
        .OnSuccess("onSuccess")
        )
    )


<script type="text/javascript">

    function onLoad(e) {
        $(this).find("input").attr("accept", "image/jpeg,image/jpg,image/png");
    }

    function onSelect(e) {

        if (e.files[0].size > 102400) {
            alert( Image Size Should Be <= 100KB );
            e.preventDefault();
            return false;
        }            

        var ext =e.files[0].extension.toLowerCase();
        if ($.inArray(ext, [ .jpeg ,  .jpg ,  .png ]) == -1) {
            alert( Only Supported Extension are jpeg/jpg, png );
            e.preventDefault();
            return false;
        }
        return true;
    }
</script>




相关问题
Discuss on Commercial Component libraries of silverlight

I am now looking forward to buy a component library of silverlight for increase the productivity. I find there are number of them. Telerik ComponentOne ComponentArt Infragistics Syncfusion I found ...

Telerik RadGrid: grid clientside pagination

I have a web service which returns me some data,I am massaging this data and using this as datasource for my radgrid (telerik). The datasource is quite large, and would like to paginate it. I found ...

Binding a radgrid to a tree like data structure

I have a structure that looks following Class TreeNode { public TreeNode Parent { get; } public IEnumerable<TreeNode> Children { get; } public . . . . } I want to bind this to a ...

IQueryable into a hierarchy

I currently have an IQueryable of Questions. In my Question object I have and "id" and a "parentId" which can be used to create a hierarchy. Currently, I bind a RadTreeView to the IQueryable of ...

Expand all items in RadGrid Hierarchy

I am using a RadGrid (2009 Q2) with a hierarchy. Is there a way in the client api to expand all rows and vice-versa? thanks! Update: I have written a javascript function based off the api ...

热门标签