js上传文件、文件大小限制
js上传文件,设置允许上传文件的大小
html:
<input type="file" value="选择" id="files1" class="forget-form" style="display:inline-table;margin-left:15px;" />
js:
//file.files 是一个fileList对象 fileList里面是file对象
var file = document.getElementById("files1").files[0];
//var file = files.files[0];
if (file) {
if (file.type != "application/zip" && file.type != "application/x-zip-compressed") {
//alert('请选择正确的zip类型文件')
show_toastr('', 'e', '请选择正确的zip类型文件', '消息提醒');
$('btn_zip_up').attr('data-v', '0');
return;
}
var fileSize = file.size / (1024 * 1024);
if (fileSize > 20) {
show_toastr('', 'e', '用户资料zip文件请不要超过20MB', '消息提醒');
return;
}
var fileSize = file.size / (1024 * 1024);
if (fileSize > 20) {
show_toastr('', 'e', '用户资料zip文件请不要超过20MB', '消息提醒');
return;
}
//return;
if (file.size > 1024 * 1024)
fileSize = (Math.round(file.size * 100 / (1024 * 1024)) / 100).toString() + 'MB';
else
fileSize = (Math.round(file.size * 100 / 1024) / 100).toString() + 'KB';
//document.getElementById('fileName').innerHTML = file.name;
}
else {
//alert('请选择要上传的文件');
show_toastr('', 'e', '请选择要上传的文件', '消息提醒');
$('btn_zip_up').attr('data-v', '0');
return;
}
var formData = new FormData();
formData.append("file", file); /*.files[0] 获取上传的图片对象*/
$.ajax({
url: '/Guest/UpdateZip',
data: formData,//{ model: obj, token: _token },
type: "post",
cache: false,
//dataType: "json",
processData: false,
contentType: false,
success: function (data) {
console.log(data);
},
fail: function (res) {
console.log(res);
},
error: function (res) {
console.log(res);
},
complete: function (res) {
}
});
上传文件大小限制说明:
在Web.config里的 <system.web>节点里添加:
<!--maxRequestLength:支持的最大文件上载大小( KB ),默认值为 4096 KB (4 MB),最大值只能是2097151KB;executionTimeout:指示在被 ASP.NET 自动关闭前,允许执行请求的最大秒数-->
<!--https://blog.csdn.net/songkexin/article/details/5571306-->
<httpRuntime targetFramework="4.5" executionTimeout= "19000" maxRequestLength="208000" useFullyQualifiedRedirectUrl= "false" minFreeThreads= "8" minLocalRequestFreeThreads= "4" appRequestQueueLimit= "100"/>
增加节点:
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="2147483647">
</requestLimits>
</requestFiltering>
</security>
</system.webServer>
后端:
HttpFileCollectionBase files = Request.Files;
HttpPostedFileBase file = null;
if (files.Count > 0)
{
file = files[0];
}
if (files.Count < 1 || file == null || file.ContentLength == 0)
{
data = "请上传客户信息(zip格式)";
return this.Json(new { succeed = succeed, data = data, ierror_step = 2, method = "UpdateIMG" });
}
string fileName = file.FileName;// DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".zip";
string path = Server.MapPath("/Admin/Excel/devUp/");//images/
if(!System.IO.Directory.Exists(path))
{
System.IO.Directory.CreateDirectory(path);
}
string savefile = System.IO.Path.Combine(path, fileName);
if(System.IO.File.Exists(savefile))
{
return this.Json(new { succeed = true, data = "已经存在该升级包了", ex = "" });
}
file.SaveAs(savefile);
data = "成功上传升级apk文件";
return this.Json(new { succeed = true, data = data, ex = "" });
更多推荐
所有评论(0)