JS obtains the path, suffix, size, and file name code of the IE Upload File.

Source: Internet
Author: User

This article first introduces a very good function for obtaining the Upload File Path in ie7 and ie8, and then detailed the code for obtaining the suffix, size, and file name under ff, for more information, see.

Compatible with ie7 and 8, get the Upload File Path

The Code is as follows: Copy code

Function validateFileSize (id, maxsize ){
Var btnsave = document. getElementById ("Button2 ");
Btnsave. disabled = false;
Var filepath = "";
Var fileupload = document. getElementById (id );
If (fileupload. value. length <5) {alert ('select a file! '); Return ;}
Var agent = window. navigator. userAgent;
If (document. all ){
Var isIE7 = agent. indexOf ('msie 7.0 ')! =-1;
Var isIE8 = agent. indexOf ('msie 8.0 ')! =-1;
// Obtain the file path from IE7 and IE8
If (isIE7 | isIE8 ){
Fileupload. select ();
Filepath = document. selection. createRange (). text;
}
// Obtain the file path from IE6
Else
{Filepath = file. value ;}
// PageMethods. ValidateFile (filepath, maxsize, ieCallBack );
Alert (filepath );
}
If (agent. indexOf ("Firefox")> = 1 ){
Alert ("FF ");
Alert (getValue ());
Return false;
If (fileupload. files ){
Var size = fileupload. files [0]. fileSize;
If (size> parseInt (maxsize )){
Fileupload. parentNode. innerHTML = '<input type = "file" name = "' + fileupload. name + '"id ="' + fileupload. id + '"onchange =" disableSave (this); "/> ';
Alert ("the file size exceeds! ");
}
Else {alert ("File Uploaded successfully! ");}
}
}
}


 

Add some code (file path, suffix, size, and file name) for file upload)

The Code is as follows: Copy code

/**
* Function Description: obtains the full path of the input type = file image.
* @ Obj input type = file object
**/
Function getFullPath (obj)
{
If (obj)
{
// Ie
If (window. navigator. userAgent. indexOf ("MSIE")> = 1)
{
Obj. select ();
Return document. selection. createRange (). text;
}
// Firefox
Else if (window. navigator. userAgent. indexOf ("Firefox")> = 1)
{
If (obj. files)
{
Return obj. files. item (0). getAsDataURL ();
}
Return obj. value;
}
Return obj. value;
}
}

 

<Input type = "file" onchange = "document. getElementById ('img '). src = getFullPath (this);">

 
 

# IE or Firefox

Function getFullPath (obj)

{

If (obj)

{

// Ie

If (window. navigator. userAgent. indexOf ("MSIE")> = 1)

{

Obj. select ();

Return document. selection. createRange (). text;

}

// Firefox

Else if (window. navigator. userAgent. indexOf ("Firefox")> = 1)

{

If (obj. files)

{

Return obj. files. item (0). getAsDataURL ();

}

Return obj. value;

}

Return obj. value;

}

}

 

 

 

# Determining the suffix

Function yulan (){

Var filePath = getFullPath (document. getElementById ('upfile '));

Var fileText = filePath. substring (filePath. lastIndexOf ("."), filePath. length );

Var fileName = fileText. toLowerCase ();

If (fileName! Your '.jpg ') & (fileName! Using '.gif ') & (fileName! Using '.jpeg ') & (fileName! Using '.png ') & (fileName! Using '.bmp '))

{

Alert ("sorry, the system only supports photos in the standard format. Please adjust the format and upload it again. Thank you! ");

Document. form1.UpFile. focus ();

}

Else

{

Document. getElementById ("preview "). innerHTML = " ";

}

}

 

 

# Determining the file size

Var Sys = {};

If (navigator. userAgent. indexOf ("MSIE")> 0 ){

Sys. ie = true;

}

If (isFirefox = navigator. userAgent. indexOf ("Firefox")> 0 ){

Sys. firefox = true;

}

 

Var maxLength = 5*1024*1024;

 

Function checkFileChange (obj ){

Var filesize = 0;

If (Sys. firefox ){

Filesize = obj. files [0]. fileSize;

} Else if (Sys. ie ){

Var filePath = obj. value;

Var image = new Image ();

Image. src = filePath;

Filesize = image. fileSize;

}

Alert (filesize );

}

Some may encounter the problem that the local path becomes "C: fakepath" When uploading files in IE.


This is no problem in IE7 and earlier IE browser versions, but it won't work when it comes to IE8. In IE8, you will only get the path "C: fakepathxxx ", xxx is your file name.

What's going on?
Originally, for security reasons, IE8 blocked the actual local file path during file uploading and replaced it with "C: fakepath.

But what should we do if we want to obtain the real local file path?

You can obtain the actual path by setting browser security options:

Internet Options-> Security-> Custom Level-> include local directory path when uploading local files to the server-> select "start"-> OK

However, as developers, we cannot expect customers to do this, so we must solve this problem through code.

For example, the HTML code of my file upload control is:

The Code is as follows: Copy code

<Input id = "file_upl" type = "file"/>

In the JS code, I can obtain the actual file path as follows:

The Code is as follows: Copy code

Var file_upl = document. getElementById ('file _ upl ');
File_upl.select ();

Var realpath = document. selection. createRange (). text;

If we use Ext, we will use Ext. ux. form. fileField component. If the id assigned to it is "file_upl", the above problem will also occur. The solution is the same, but we can get the input with the type of file, the elements obtained based on "file_upl" are not what we need at all. Ext automatically generates an input with the type of file. You can see this input through DebugBar or other similar tools, its id is assigned to Ext. ux. form. fileField id with the suffix "-file", that is, "file_upl-file", so get the true file path
The JS Code is:

The Code is as follows: Copy code

Var file_upl = document. getElementById ('file _ upl-file ');
File_upl.select ();

Var realpath = document. selection. createRange (). text;

Related Article

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.