Javascrip client to verify file size and file type and reset upload _ form effects

Source: Internet
Author: User
The following is a generic Javascrip script I wrote, although the calls need to be given more parameters, but are actually needed to use. You can refer to it and change it to your own script.


Copy Code code as follows:



/***** get file information edit by ZHAOGW reference by misssionotherattedit.jsp*****/


/*file:input type= The object of "file", generally with this.


VType: An object name used to record file type information for a file. Typically the input object.


Divtype: The name of a Div object. Use its innerHTML content to display the type information of the file.


Vfile: An object name that is used to log file name information. Typically the input object.


Divfile: The name of a Div object. Use its innerHTML content to display file name information.


Vsize,divsize is similar to the above, but only records the size of the file information.


Mmaxsize: Calculates the maximum file size that is allowed to be uploaded in M.


Allowtype: Only file types accepted


*/


function GetFileInfo (file,mmaxsize,allowtype,vfile,divfile,vtype,divtype,vsize,divsize) {


var filePath = file.value;//file path


var filename;//filename


var filetype;//file type


var tmpobj;//temporary Object


var notallowtype=new Array ("EXE", "Bat", "ASP", "JSP", "JS", "DLL");


var Mhtml=document.getelementbyid (file.name+ ' Td '). InnerHTML;


/*


var mhtml= "<input type= ' file ' name= '" +


file.name+ "' class= ' input100 ' id= '" +file.id+


"' tip= ' cannot upload empty file ' contenteditable= ' false ' tmt:required= ' true ' focustips= ' please enter filename ' onchange= ' getfileinfo (this," +


Mmaxsize+ ", \" "+allowtype+" \ ", \" "" +vfile+ "\", "" "+divfile+" \ ", \" "+vtype+" \ ", \" "+divtype+" \ "," "" +vsize+ "\", "" "+ Divsize+ "\") ' > ';


*/


alert (MHTML);


Get file name


if (FilePath!= null && filePath!= ") {


var pass=true;


File type


FileType = filepath.substring (Filepath.lastindexof ('. ') + 1,filepath.length);


if (filetype!=null&&filetype!= ')


{


for (var i in Notallowtype) {


if (Filetype.tolowercase () ==notallowtype[i]) {


Pass=false;


break;


}


}


Allowed types, empty words do not set the allowed type


var Match=false;


if (allowtype!=null&&allowtype!= ') {


var allowlist=allowtype.split (' | ');


for (Var j in AllowList) {


if (Filetype.tolowercase () ==allowlist[j].tolowercase ()) {


Match=true;


break;


}


} else {match=true;}


if (Pass&&match)


{


FileName = filepath.substring (Filepath.lastindexof (' \ ") + 1,filepath.length);


Tmpobj=document.getelementbyid (VType);


if (tmpobj!=null)


Tmpobj.value = FileType;


Tmpobj=document.getelementbyid (Divtype);


if (tmpobj!=null)


tmpobj.innerhtml = FileType;


Tmpobj=document.getelementbyid (Vfile);


if (tmpobj!=null)


Tmpobj.value = FileName;


Tmpobj=document.getelementbyid (Divfile);


if (tmpobj!=null)


tmpobj.innerhtml = FileName;


try{


var fso,f,s;


FSO = new ActiveXObject ("Scripting.FileSystemObject");


f = fso. GetFile (File.value);


if (F.size > mmaxsize*1048576) {


Alert ("File size cannot exceed" +mmaxsize+ "M");


document.getElementById (file.name+ ' Td '). InnerHTML = MHTML;


Tmpobj=document.getelementbyid (VType);


if (tmpobj!=null)


Tmpobj.value = ';


Tmpobj=document.getelementbyid (Divtype);


if (tmpobj!=null)


tmpobj.innerhtml = ';


Tmpobj=document.getelementbyid (Vfile);


if (tmpobj!=null)


Tmpobj.value = ';


Tmpobj=document.getelementbyid (Divfile);


if (tmpobj!=null)


tmpobj.innerhtml = ';


Tmpobj=document.getelementbyid (vsize);


if (tmpobj!=null)


Tmpobj.value = ';


Tmpobj=document.getelementbyid (divsize);


if (tmpobj!=null)


tmpobj.innerhtml = ';


Return


}


Else


{


Tmpobj=document.getelementbyid (vsize);


if (tmpobj!=null)


Tmpobj.value = f.size;


Tmpobj=document.getelementbyid (divsize);


if (tmpobj!=null)


tmpobj.innerhtml = f.size+ "byte (byte)";


var imgtype=new Array ("JPG", "JPEG", "GIF", "BMP");


var Isimg=false;


File type


if (filetype!=null&&filetype!= ')


{


For (var k in Imgtype) {


if (Filetype.tolowercase () ==imgtype[k]) {


Isimg=true;


break;


}


}


var Tmpobj=document.getelementbyid ("Imgview");


if (isimg&&tmpobj) {


var y = document.getElementById (file.name+ "img");


if (y) {


Y.SRC = "file://localhost/" + file.value;


}else{


var img=document.createelement ("img");


Img.setattribute ("src", "file://localhost/" +file.value);


Img.setattribute ("width", "120");


Img.setattribute ("Height", "90");


Img.setattribute ("id", file.name+ "img");


Tmpobj.appendchild (IMG);


}


}}


}catch (e) {


Ignore


}


}


else if (!pass) {alert ("not allowed to upload file type:" +filetype);d Ocument.getelementbyid (file.name+ ' Td '). InnerHTML = MHTML;}


else if (!match) {alert ("Only allow upload file types: +allowtype);d Ocument.getelementbyid (file.name+ ' Td '). InnerHTML = MHTML;}


}


}





Code that invokes the method:


Copy Code code as follows:

<TD width= "23%" class= "Inputtd" id= "filenametd" colspan= "3" >
<input type= "file" name= "filename" class= "input100" id= "filename" tip= "Cannot upload empty file" usage= "Notempty" contenteditable= " False "tmt:required=" true "focustips=" Please enter the filename "onchange=" GetFileInfo (this,10, ', ' vo.filename ', ' fileNameTd2 ', ' Vo.filetype ', ' filetypediv ', ' vo.filesize ', ' Filesizediv ') >
<input type= "hidden" name= "Vo.filename" id= "Vo.filename" >
<span id= "Filenamediv" name= "Filenamediv" ></span>
</td>



Need to display the current upload picture needs to add the following code:





&lt;div id= "Imgview" &gt; &lt;/div&gt;





Simple description of the relevant conventions for scripting:





1: If you must use &lt;td&gt; object to (or other objects with the innerHTML attribute to contain the object of input type= "file", and the name must be input's Name property + "TD" as the suffix)


2:imgview is dead, because he doesn't want to add more parameters, so this is fixed here. You can also refer to the name of the parameter passed in. It's convenient to see you all.


3: All parameters can be "but the first argument is basically this." The script will automatically determine the relevant parameters.

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.