Asp.Net 上传前检查文件大小 [英] Asp.Net Check file size before upload

查看:22
本文介绍了Asp.Net 上传前检查文件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在使用 asp fileupload 组件上传文件时之前检查选定的文件大小.我无法使用 activex,因为该解决方案必须适用于每个浏览器(firefox、Chrome 等)

I want to check the selected file size BEFORE uploading a file with the asp fileupload component. I can not use activex because the solution have to works on each browser (firefox, Chrome, etc..)

我该怎么做?

感谢您的回答..

推荐答案

ASPX

<asp:CustomValidator ID="customValidatorUpload" runat="server" ErrorMessage="" ControlToValidate="fileUpload" ClientValidationFunction="setUploadButtonState();" />
<asp:Button ID="button_fileUpload" runat="server" Text="Upload File" OnClick="button_fileUpload_Click" Enabled="false" />
<asp:Label ID="lbl_uploadMessage" runat="server" Text="" />

jQuery

function setUploadButtonState() {

   var maxFileSize = 4194304; // 4MB -> 4 * 1024 * 1024
   var fileUpload = $('#fileUpload');

   if (fileUpload.val() == '') {
    return false;
   }
   else {
      if (fileUpload[0].files[0].size < maxFileSize) {
         $('#button_fileUpload').prop('disabled', false);
         return true;
      }else{
         $('#lbl_uploadMessage').text('File too big !')
         return false;
      }
   }
}

这篇关于Asp.Net 上传前检查文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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