JS get IE Upload file path, suffix, size, file name code

Source: Internet
Author: User
Tags file upload

Compatible ie7,8 get 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 (' Please select 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;
IE7 and IE8 get file paths
if (isIE7 | | isIE8) {
Fileupload.select ();
filepath = Document.selection.createRange (). text;
}
IE6 Get file path
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 + ' "Onchang E= "Disablesave (this);" /> ';
Alert ("File exceeds size!") ");
}
else {alert ("File Upload succeeded!") "); }
}
}
}


Add some code on file upload (upload file path, suffix, size, filename)

The code is as follows Copy Code

/**
* Function Description: Get input type=file image full path
* @obj Input Type=file objects
**/
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还是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;

}

}

#判断后缀名

function Yulan () {

var filePath =getfullpath (document.getElementById (' upfile '));

var filetext =filepath.substring (Filepath.lastindexof ("."), filepath.length);

var fileName =filetext.tolowercase ();

if ((filename!= '. jpg ') && (filename!= '. gif ') && (filename!= '. jpeg ') && (filename!= '. png ') && (filename!= '. bmp '))

{

Alert ("Sorry, the system only supports the standard format of the photos, please adjust the format after uploading again, thank you!") ");

Document.form1.UpFile.focus ();

}

Else

{

document.getElementById ("Preview"). innerhtml= " ';

}

}

#判断文件大小

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 friends may encounter the problem that the local path becomes "C:fakepath" when the Internet Explorer uploads the file.


This is not a problem in the IE7 and previous versions of IE browser, but to IE8 above, it is not, in IE8 you will only get this path: "C:fakepathxxx", where xxx is your file name.

What the hell is going on here?
Originally, IE8 for security reasons, upload files shielding the real local file path, and "C:fakepath" replaced it.

But what do we do to get the real local file path?

You can get the real path by setting the browser security option:

Internet Options-> Security-> Custom level-> include local directory paths when uploading local files to the server-> select Startup-> determine

But, as developers, we can't expect our clients to do this, so we have to solve the problem through code.

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

The code is as follows Copy Code

<input id= "FILE_UPL" type= "file"/>

So in the JS code I can do this to get the real file path:

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 are using EXT, then we will use the Ext.ux.form.FileField component, assuming that the ID that we assign to it is "FILE_UPL", then the problem will also arise, the solution is the same, But we get to type for the input of file, and according to "FILE_UPL" to get the element is not what we need, ext is automatically generated a type file input, You can see this input through Debugbar or other similar tools, its ID is assigned to Ext.ux.form.FileField ID plus "-file" suffix, that is, "file_upl-file", so get the real file path
The JS code is:

  code is as follows copy code

var file_ UPL = document.getElementById (' File_upl-file ');
File_upl.select ();

var realpath = Document.selection.createRange (). text;

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.