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?
- <Input id = "upload_img" type = "file" onchange = "onuploadimgchange (this)"/>
<input id="upload_img" type="file" onchange="onUploadImgChange(this)" />
[JavaScript]View plaincopyprint?
- 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 );
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?
- 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])
- }
- }
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?
- <Input id = "upload_img" type = "file" onchange = "onuploadimgchange (this)"/>
- <SCRIPT type = "text/JavaScript">
- Function onuploadimgchange (fileinput ){
- VaR filepath = fileinput. value;
- VaR fileext = filepath. substring (filepath. lastindexof ("."))
- . Tolowercase ();
- If (! Checkfileext (fileext ))
- {
- Alert ("the file you uploaded is not an image. please upload it again! ");
- Return;
- }
- 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])
- }
- } Else {
- Fileinput. Select ();
- VaR url = Document. selection. createRange (). text;
- Try {
- VaR FSO = new activexobject ("scripting. FileSystemObject ");
- } Catch (e ){
- Alert ('If you are using IE, please lower the security level! ');
- }
- Alert ("file size:" + FSO. GetFile (URL). size );
- }
- }
- Function checkfileext (EXT)
- {
- If (! Ext. Match (/. jpg |. gif |. PNG |. BMP/I )){
- Return false;
- }
- Return true;
- }
- </SCRIPT>