During file upload, in order to achieve the asynchronous upload effect, iframe is usually used for file upload, but we cannot process the data returned by the server like ajax, so as to determine the file size and file style, we generally think that when we upload files, in order to achieve the asynchronous upload effect, generally, iframe is used to upload files, but we cannot process the data returned by the server like ajax to determine the file size and file style, therefore, we generally think of using js to make a preliminary judgment on the size and format of the uploaded file, and then make another judgment on the server (to prevent the browser from refusing to execute the script file ). The following provides a method to determine the file size using js. 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: 2097152, 2: 5242880, 3: 20971520}; var errMsg = {" 0 ": 'the image format is incorrect.
The '+' audio format is incorrect.
The '+' video format is incorrect.
', "1": 'incorrect image format', "2": 'incorrect audio format', "3": 'incorrect video format '}; var errSizeMsg = {'1': 'image file less than 2m', '2': 'audio file 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 = "") {// checks alert again before submission ('select the file to be uploaded '); return false;} if (! CheckFileType (filename, type) {alert ('file format incorrect '); 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 is the id of the upload control, obj is the upload control itself (this)
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.