Use the upload crit to verify the size of the uploaded file

Source: Internet
Author: User

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.



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.