文件大小限制问题,限制上传文件功能只能上传指定大小的文件
二话不说直接上代码:
<form id="form_uploadexcel" action="/DB/Edit/UploadExcel" method="post" enctype="multipart/form-data"> <table width="440" border="0" cellpadding="0" cellspacing="0" class="table_none"> <tr> <td style="width: 130px" align="right"><strong>标题:<></td> <td style="width:310px"> <input type="text" id="fileSubject" name="subject" class="easyui-validatebox" required="true" style="width: 280px" /> <strong style="color:#f00">*<> </td> </tr> <tr> <td style="width: 130px" align="right" valign="top"><strong>描述:<></td> <td> <textarea type="text" id="fileComment" name="comment" class="easyui-validatebox" style="width: 280px;max-width:280px; height: 90px;max-height:90px"></textarea> </td> </tr> <tr> <td style="width: 130px" align="right" valign="top"><strong>数据文件:<></td> <td> <input id="excelFile" name="excelFile" type="file" accept=".xls,.xlsx" style="width:280px" onchange="fileChange(this,1)" /> </td> </tr> <tr> <td colspan="2" align="right" style="padding-top:20px"> <a href="/edit/downloadtemplate/@ViewBag.DatabaseId" class="easyui-linkbutton" data-options="iconCls:'icon-download'">下载模板</a> <a href="javascript:void(0)" class="easyui-linkbutton width_80" data-options="iconCls:'icon-save'" onclick="uploadExcel()">上传</a> <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save'" onclick="submitApproval()">上传并提交审核</a> </td> </tr> </table> <input type="hidden" id="hdDatabaseId" name="databaseid" value="@ViewBag.DatabaseId" /> <input type="hidden" id="hdRecordstatus" name="recordstatus" value="0" /> </form> <input type="hidden" id="hidFileAllowType_1" value=".xls,.xlsx" /> // function fileChange(target, filetypeid) { var filepath = target.value; if (filepath == "") { return false; } var fileSize = 0; var isIE = /msie/i.test(navigator.userAgent) && !window.opera; if (isIE && !target.files) { var fileSystem = new ActiveXObject("Scripting.FileSystemObject"); var file = fileSystem.GetFile(filepath); fileSize = file.Size; } else { fileSize = target.files[0].size; } var size = fileSize / 1024; if (size > 61440) { target.value = ''; $.messager.alert('提示', '文件大小不能大于60M!', 'warning'); return false; } if (size <= 0) { target.value = ''; $.messager.alert('提示', '文件大小不能为0M!', 'warning'); return false; } fsize = size; var ss = $('#hidFileAllowType_' + filetypeid).val(); if (ss != '') { var arrFileAllowType = ss.split(','); var fileExtension = filepath.substring(filepath.lastIndexOf('.'), filepath.length); var strAllowType = ""; var isAllowType = false; for (var i = 0; i < arrFileAllowType.length; i++) { strAllowType += arrFileAllowType[i]; if (arrFileAllowType[i].toString().toLowerCase() == fileExtension.toLowerCase()) { isAllowType = true; } } if (!isAllowType) { target.value = ''; $.messager.alert("提示", "只能上传以下格式的文件:" + strAllowType); return false; } } var filename = filepath.substring(filepath.lastIndexOf('\\') + 1); filename = filename.substring(0, filename.lastIndexOf('.')); $('input[name=filename]').val(filename); return true; }
web配置:
<system.web> <httpRuntime maxRequestLength="2097151" maxQueryStringLength="2097151" maxUrlLength="2097151" executionTimeout="3600" requestValidationMode="2.0" /> </system.web>
<system.webServer> <security> <requestFiltering> <requestLimits maxQueryString="4080" maxAllowedContentLength="2147483647" /> </requestFiltering> </security> </system.webServer>