In the background Code, although filecontent. length can be used to obtain the size, this method should be avoided as much as possible.
The server-side code will run on the server side, so the verification using this method will find it very slow. Because the page first uploads the file to the temporary space on the server, and the server obtains the file size. Therefore, the efficiency is very low, which seriously occupies the bandwidth. Method is not available. In particular, when the network speed is poor, a prompt is displayed after the customer uploads the file for a long time. It is strange not to crash.
Therefore, JS Code is preferred.
The following is the text:
First, add a fileupload control on the page. (Taking the ID fudpolicy as an example again)
Then add an upload button. Sample interface.
<Script language = "JavaScript" type = "text/JavaScript">
// Verify the file size
Function ckfilesize ()
{
// Pre-load the attachment as an image processing result in the buffer zone
Var file = new image ();
// Set the URL of the Attachment
File. DYNSRC = Document. getelementbyid ("fudpolicy"). value;
// Obtain the size of the uploaded file
VaR filesize = file. filesize/1024;
If (filesize> 3072)
{
Alert ("the uploaded file cannot be larger than m ");
Return false;
}
Return true;
}
</SCRIPT>
Finally, you can call it in the onload event.
IE6 passed the test, and IE7 refused to access the service.
The following code can be used in IE7:
- <SCRIPT type = "text/JavaScript">
- FunctionGetfilesize (filepath)
- {
- VaRFSO =NewActivexobject ("scripting. FileSystemObject ");
- Alert ("file size:" + FSO. GetFile (filepath). size );
- }
- </SCRIPT>
- <Body>
- <Input type = "file" name = "file" size = "30" onchange = "getfilesize (this. Value);">
- </Body>
This method can be implemented and easily thought by developers, but the only drawback is that there is a security prompt. Of course, change the file name. HTA will block the security prompt, but it is difficult to obtain it.Not recommended, not to mention
<Form ID = "form1" runat = "server" onsubmit = "Return ckfilesize ()">
Next, let's talk about another method. There is an IMG tag in the HTML Tag that is not "deep" for general developers. let's first talk about its attributes: SRC, DYNSRC, start, ALT, controls, loop, loopdelay, hspace, vspace .... some commonly used attributes will not be listed. Here we will talk about the "DYNSRC" attribute: DYNSRC can be used to insert various multimedia, the format can be WAV, Avi, AIFF, AU, MP3, Ra, ram, and so on. The URL is an audio or video file and its path, which can be a relative or absolute path.
Example:
In this way, the path of any type of file can be dynamically assigned based on DYNSRC. In JavaScript, the file size can be obtained based on The filesize attribute of the image object. Of course, the image object has several other attributes, such as filecreateddate, filemodifieddate, filesize, fileupdateddate, and filters.... The Code is as follows:
JavaScript code
<SCRIPT type = "text/JavaScript">
Function getfilesize (filepath)
{
VaR image = new image ();
Image. DYNSRC = filepath;
Alert (image. filesize );
}
</SCRIPT>
<Body>
<Input type = "file" name = "file" size = "30" onchange = "getfilesize (this)">
</Body>
<SCRIPT type = "text/JavaScript">
Function getfilesize (fileobj)
{
VaR image = new image ();
Image. DYNSRC = fileobj. value;
Alert (image. filesize | fileobj. Files [0]. filesize );
}
</SCRIPT>
<Body>
<Input type = "file" name = "file" size = "30" onchange = "getfilesize (this. Value)">
</Body>
JavaScript to determine the file type
VaR type = (SRC. substr (SRC. lastindexof ("."). tolowercase ();
If (type! = ". Jpg" & type! = ". GIF" & type! = ". Jpeg" & type! = ". PNG") {alert ("the image you upload does not match (.jpg |. JPEG |. gif |. PNG )! "); Return false ;}