We do file upload, in order to achieve the effect of asynchronous uploads, will generally choose to use the form of IFrame to upload files, but we can not like Ajax to the server-side returned data processing, so as to file size and file style judgments, So we generally think of using JS to upload the file size and format to make a preliminary judgment, at the server to make another decision (to prevent the browser from refusing to execute script files).
The following provides a way to use JS to determine the file size.
Copy Code code as follows:
var url = window.location.href, type = Url.substr (Url.lastindexof ('/') +1);
Console.log (type);
var Allowtype = {
". bmp": 1, ". png": 1, ". JPEG": 1, ". jpg": 1, ". gif": 1,
". mp3": 2, ". wma": 2, ". wav": 2, ". Amr": 2,
". RM": 3, ". RMVB": 3, ". wmv": 3, ". avi": 3, ". mpg": 3, ". MPEG": 3, ". mp4": 3
};
var allowsize = {1:2,097,152, 2:5,242,880, 3:20,971,520};
var errmsg = {
"0": ' Picture format not correct <br/> '
+ ' audio format not correct <br/> '
+ ' video format not correct <br/> ',
"1": ' Picture format is not correct ',
"2": ' Audio format is not correct ',
"3": ' Video format is not correct '
};
var errsizemsg = {
' 1 ': ' picture file less than 2M ',
' 2 ': ' Audio file is less than 5M ',
' 3 ': ' Video file less than 20M ',
}
function checkfiletype (filename, type) {
var ext = filename.substr (Filename.lastindexof (".")). toLowerCase (),
res = Allowtype[ext];
if (type = = 0) {
Return!! Res
} else {
return type = = Res;
}
}
function checkfilesize (target, size) {
var Isie =/msie/i.test (navigator.useragent) &&!window.opera;
var fileSize = 0;
if (Isie &&!target.files)
{
var filePath = Target.value;
var filesystem = new ActiveXObject ("Scripting.FileSystemObject");
var file = Filesystem.getfile (FilePath);
fileSize = file. Size;
} else {
FileSize = target.files[0].size;
}
var fsize = filesize/1024*1024;
if (parseint (fsize) >= parseint (size)) {
return false;
}else{
return true;
}
}
function upload (obj) {
var filename = Jquery.trim (jQuery (' #uploadFile '). Val ());
if (!filename | | filename = = "") {//re-test before submitting
Alert (' Select File to upload ');
return false;
}
if (!checkfiletype (filename, type)) {
Alert (' Incorrect file format ');
return false;
}
var ext = filename.substr (Filename.lastindexof (".")). toLowerCase ();
var res = Allowtype[ext];
if (!checkfilesize (Obj,allowsize[res])) {
Alert (Errsizemsg[res]);
return false;
}
Other processing
}
UploadFile for the upload control's id,obj for the upload control itself (this)