Javascrip client verifies the file size and file type and resets the upload

Source: Internet
Author: User

The following is a general commit Crip script I wrote. Although many parameters need to be assigned during the call, they are all actually needed. You can refer to it and change it to the script you need.
Copy codeThe Code is as follows:
/***** Obtain the file information. For details about edit by zhaogw, see by misssionOtherAttEdit. jsp *****/
/* File: the object of input type = "file". this is generally used.
VType: an object name used to record the file type information of a file. It is generally an input object.
DivType: name of a Div object. Use the innerHTML content to display the file type information.
VFile: an object name used to record the file name information. It is generally an input object.
DivFile: name of a Div object. Use the innerHTML content to display the file name information.
VSize and DivSize are similar to the preceding one. They only record the file size information.
MMaxSize: calculates the maximum file size that can be uploaded in MB.
AllowType: only acceptable file types
*/
Function getFileInfo (file, mMaxSize, allowType, vFile, DivFile, vType, DivType, vSize, DivSize ){
Var filePath = file. value; // file path
Var fileName; // file name
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 = 'unable to upload an empty file 'contenteditable = 'false' tmt: required = 'true' focusTips =' enter the file name '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 type. If it is null, the allowed type is not set.
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 ("the 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 (bytes )";
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 ("the file type cannot be uploaded:" + fileType); document. getElementById (file. name + 'td '). innerHTML = mHTML ;}
Else if (! Match) {alert ("only allowed file types to be uploaded:" + allowType); document. getElementById (file. name + 'td '). innerHTML = mHTML ;}
}
}

Code for calling a method:
Copy codeThe Code is as follows:
<Td width = "23%" class = "inputTd" id = "fileNameTd" colspan = "3">
<Input type = "file" name = "fileName" class = "input100" id = "fileName" Tip = "unable to upload an empty file" Usage = "notempty" contentEditable = "false" tmt: required = "true" focusTips = "Enter the file name" onChange = "getFileInfo (this, 10,'', 'vo. fileName ', 'filenamed2', 'vo. filetype', 'filetypediv ', 'vo. filesize', 'filesizediv ') ">
<Input type = "hidden" name = "vo. fileName" id = "vo. fileName">
<Span id = "fileNameDiv" name = "fileNameDiv"> </span>
</Td>

To display the currently uploaded image, add the following code:

<Div id = "imgView"> </div>

Briefly describe the related conventions of the script:

1: If you must use the <td> Object (or other objects with the innerHTML attribute to contain the input type = "file" object, and the name must be the name attribute of input + "Td" as the suffix)
2: imgView writing is dead. Because you don't want to add more parameters, this is fixed here. You can also pass the name as a parameter. It's easy to see.
3: All parameters can be '', but the first parameter is basically this. The script automatically determines the relevant parameters.

Related Article

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.