Asp.net
Asp.Net 在上傳前檢查文件大小
我想在使用 asp fileupload 組件上傳文件**之前檢查選定的文件大小。**我不能使用 activex,因為該解決方案必須適用於每個瀏覽器(firefox、Chrome 等)
我怎樣才能做到這一點 ?
謝謝你的回答。。
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; } } }