html代码如下:
<form action="后端接口" method="post" id="uploads">
<div class="fileCon">
<input input="file" id="import" name="attachmentNew" onchange="fileChange(this);"/>
<g:actionSubmit id="uploadfile" value="上传" action="_action_file" class="btn btn-sm"/>
</div>
</form>
js代码如下:
function fileChange(target) {
var fileSize = 0;
fileSize = target.files[0].size;
var size = fileSize / 1024;
if(size>1000){
alert("附件不能大于1M");
target.value="";
return false; //阻止submit提交
}
var name=target.value;
var fileName = name.substring(name.lastIndexOf(".")+1).toLowerCase();
if(fileName !="jpg" && fileName !="jpeg" && fileName !="pdf" && fileName !="png" && fileName !="dwg" && fileName !="gif" && fileName !="xls" && fileName !="xlsx" && fileName !="word" && fileName !="doc"&& fileName !="docx" && fileName !="txt"){
alert("请选择图片格式文件上传(jpg,png,gif,dwg,pdf,gif等)!");
target.value="";
return false; //阻止submit提交
}
}