JS restricts the size of locally uploaded files (supports IE6, Firefox Google)

Source: Internet
Author: User

First, ie must use activexobject to access local files. This requires lowering the security level of the browser. Then the key is how to obtain the local path in IE8.

 

[HTML]View plaincopyprint?
  1. <Input id = "upload_img" type = "file" onchange = "onuploadimgchange (this)"/>
<input id="upload_img" type="file" onchange="onUploadImgChange(this)" />  

 

 

[JavaScript]View plaincopyprint?
  1. Fileinput. Select ();
  2. VaR url = Document. selection. createRange (). text;
  3. $ ('# Imagetest'). ATTR ('src', URL );
  4. Try {
  5. VaR FSO = new activexobject ("scripting. FileSystemObject ");
  6. }
  7. Catch (E)
  8. {
  9. Alert ('If you are using IE8, please lower the security level! ');
  10. }
  11. Alert ("file size:" + FSO. GetFile (URL). size );
Fileinput. select (); var url = document. selection. createRange (). text; $ ('# imagetest '). ATTR ('src', URL); try {var FSO = new activexobject ("scripting. fileSystemObject ");} catch (e) {alert ('if you are using IE8, please lower the security level! ');} Alert ("file size:" + FSO. GetFile (URL). size );

For Firefox, Google, and other new-generation browsers, fileinput. files can be used to obtain the object array of file.

 

 

[JavaScript]View plaincopyprint?
  1. If (fileinput. Files & fileinput. Files [0]) {
  2. Alert (fileinput );
  3. Alert (fileinput. Files [0])
  4. Alert ('file size you selected '+ fileinput. Files [0]. size );
  5. VaR xx = fileinput. Files [0];
  6. For (var I in XX)
  7. {
  8. Alert (XX [I])
  9. }
  10. }
If (fileinput. files & fileinput. files [0]) {alert (fileinput); alert (fileinput. files [0]) Alert ('file size you selected '+ fileinput. files [0]. size); var xx = fileinput. files [0]; for (var I in XX) {alert (XX [I])}

The following is an example of a completed test.

 

[HTML]View plaincopyprint?
  1. <Input id = "upload_img" type = "file" onchange = "onuploadimgchange (this)"/>
  2. <SCRIPT type = "text/JavaScript">
  3. Function onuploadimgchange (fileinput ){
  4. VaR filepath = fileinput. value;
  5. VaR fileext = filepath. substring (filepath. lastindexof ("."))
  6. . Tolowercase ();
  7. If (! Checkfileext (fileext ))
  8. {
  9. Alert ("the file you uploaded is not an image. please upload it again! ");
  10. Return;
  11. }
  12. If (fileinput. Files & fileinput. Files [0]) {
  13. Alert (fileinput );
  14. Alert (fileinput. Files [0])
  15. Alert ('file size you selected '+ fileinput. Files [0]. size );
  16. VaR xx = fileinput. Files [0];
  17. For (var I in XX ){
  18. Alert (XX [I])
  19. }
  20. } Else {
  21. Fileinput. Select ();
  22. VaR url = Document. selection. createRange (). text;
  23. Try {
  24. VaR FSO = new activexobject ("scripting. FileSystemObject ");
  25. } Catch (e ){
  26. Alert ('If you are using IE, please lower the security level! ');
  27. }
  28. Alert ("file size:" + FSO. GetFile (URL). size );
  29. }
  30. }
  31. Function checkfileext (EXT)
  32. {
  33. If (! Ext. Match (/. jpg |. gif |. PNG |. BMP/I )){
  34. Return false;
  35. }
  36. Return true;
  37. }
  38. </SCRIPT>

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.