如何在asp.net ajaxtoolkit ajaxfileupload控制上传多个文件之前检查每个文件的文件大小? [英] How to check file size of each file before uploading multiple files in ajaxtoolkit ajaxfileupload control in asp.net?

查看:518
本文介绍了如何在asp.net ajaxtoolkit ajaxfileupload控制上传多个文件之前检查每个文件的文件大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<cc1:AjaxFileUpload ID="AjaxFileUpload1"  AllowedFileTypes="jpg,jpeg"
runat="server" MaximumNumberOfFiles="4"  OnUploadComplete="AjaxFileUpload1_UploadComplete" 
         />

隐藏文件code

Code behind file

protected void AjaxFileUpload1_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
    {
        if (e.FileSize > 10)
        {
            string filePath = e.FileName;
            AjaxFileUpload1.SaveAs(Server.MapPath(filePath));
        }
        else
        {

        }
    }

我要检查这些文件上传事件之前的所有文件大小不得超过一个特定的值。

I want to check that all the files size should not exceed a particular value before the files upload event.

推荐答案

试试这个方法:

服务器端:

protected void AjaxFileUpload1_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
{
    try
    {
        string savePath = MapPath("~/Images/" + e.FileName);
        // dont save file & return if condition not matched.
        if (e.FileSize > 72000) // use same condition in client side code
        {
            return;
        }
        AjaxFileUpload1.SaveAs(savePath);
    }
    catch (Exception ex)    
    {
        throw ex;
    }
}

和在客户端:

<script type="text/javascript">
    function UploadComplete(sender, args) {
        var filesize = args.get_fileSize();
        var fileId = args.get_fileId();

        var status = document.getElementById('AjaxFileUpload1_FileItemStatus_' + fileId);
        var container = document.getElementById('AjaxFileUpload1_FileInfoContainer_' + fileId);


        if (filesize > 72000) { // same condition used for server side
            document.getElementById('lblStatus').innerText = "error";
            if (status.innerText) {
                status.innerText = " (Error)";
            }
            if (status.textContent) {
                status.textContent = " (Error)";
            }
            container.style.color = 'Red';
        }
    }
</script>

<cc1:AjaxFileUpload ID="AjaxFileUpload1"  AllowedFileTypes="jpg,jpeg" runat="server" MaximumNumberOfFiles="4"  OnUploadComplete="AjaxFileUpload1_UploadComplete" OnClientUploadComplete="UploadComplete" />

希望这有助于!

这篇关于如何在asp.net ajaxtoolkit ajaxfileupload控制上传多个文件之前检查每个文件的文件大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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