JS limits the type and size of uploaded files

Source: Internet
Author: User

var maxsize = 6*1024*1024;//6m
var errmsg = "Uploaded attachment file cannot exceed 6m!!! ";
var tipmsg = "Your browser does not support the calculation of upload file size, make sure to upload files not more than 6M, we recommend using IE, FireFox, Chrome browser." ";
var browsercfg = {};
var ua = window.navigator.userAgent;
if (Ua.indexof ("MSIE") >=1) {
Browsercfg.ie = true;
}else if (Ua.indexof ("Firefox") >=1) {
Browsercfg.firefox = true;
}else if (Ua.indexof ("Chrome") >=1) {
Browsercfg.chrome = true;
}

Function Checkfile () {
try{
var obj_file = document.getElementById ("Instructionname");
if (obj_file.value== ") {
Alert (" Please select Upload file ");
return;
}
if (!/\. doc|xls|pdf|xlsx|pptx|docx|ppt) $/.test (obj_file) {
Alert ("Upload file can only be doc|xls|pdf|xlsx|pptx|docx|ppt as suffix file");
$ ("#instructionName"). Val (');
return;
}
var filesize = 0;
if (Browsercfg.firefox | | browsercfg.chrome) {
FileSize = obj_file.files[0].size;
}else if (browsercfg.ie) {
var obj_img = document.getElementById (' tempimg ');
Obj_img.dynsrc=obj_file.value;
FileSize = obj_img.filesize;
}else{
Alert (tipmsg);
$ ("#instructionName"). Val (');
return;
}
if (filesize==-1) {
alert (tipmsg);
return;
}else if (filesize>maxsize) {
alert (errmsg);
return;
}else{
document.getElementById ("Bigsmall"). Value=filesize;
return;
}
}catch (e) {
alert (e);
}
}

<input id= "Instructionname" type= "file" Name= "Instructionname" value= "" onchange= "Checkfile ()" style= "width:80"/ >

<input type= "hidden" id= "Bigsmall" name= "Bigsmall" value= ""/>

JS limits the type and size of uploaded files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.