JS judge upload file size and format code _javascript tips

Source: Internet
Author: User
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)

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.