https://blog.csdn.net/u014236259/article/details/52885591
js是看到别人博客写的,直接复制下来的,然后修修补补,虽然很多没用,但是也比自己写方便
为什么需要这个,服务器是Nginx的,Nginx限制了上传文件的大小,最开始报413的错误,要求用代码解决,不改Nginx配置文件
window.check = function () { var input = document.getElementById("file"); if (input.files) { //读取图片数据 var f = input.files[0]; var reader = new FileReader(); if (f.size > 1 * 1024 * 1024) { alert("请上传小于1M的图片!"); return false; } } };
<form class="form_t" action="<?php echo Url::toRoute(['device/create']); ?>"method="post" enctype="multipart/form-data" οnsubmit="return check()">
<div class="lin_input"> <div class="remark_o"><span class="remark_i"> </span></div> <div class="label_t"><span class="label_i w4">添加样片</span></div> <div class="input_o"> <input type="file" id="file" name="imgFile">(请上传小于1M的图片) </div> </div>