For Web developers, file upload is often encountered, and file size judgment is also indispensable. Many people do not want to go to the background for judgment, because if the judgment is passed to the background, if the file is too large, an error is reported, it also involves efficiency and data echo. If you can use JavaScript on the front-end to determine the file size, it would be better.
There are two methods to summarize online:
The first method is to apply ActiveX controls, for example:
JavaScript code
<SCRIPT type = "text/JavaScript">
Function getfilesize (filepath)
{
VaR FSO = new activexobject ("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 you need to change the security settings of the browser. Otherwise, the script error "The Automation server cannot create objects" is reported. Change the browser's security settings to "medium", and set ActiveX settings to enable.
This method is not safe and is not recommended.
The second method is implemented using the DYNSRC attribute of IMG.
There is an IMG tag that is not generally known to developers in HTML tags. let's first talk about its attributes: SRC, DYNSRC, start, ALT, controls, loop, loopdelay, heatace, vspace .... some common 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, and the file size can be obtained in Javascript 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. Value)">
</Body>
<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. Value)">
</Body>
I did not test this method successfully. I used IE8, prompting that the DYNSRC attribute cannot be set.
To sum up, using JavaScript to operate files is neither secure nor compatible. I think the reason for this is that if Javascript can operate your local file system freely, security is greatly reduced.
It is best not to use this method to determine whether the size is safe and convenient in the background.