开发者

<input type='file'> IE Gives full path, FF gives only filename (or directory browse) [duplicate]

This question already has answers here: Closed 13 years ago.

Possible Duplicate:

Can’t get the complete address while uploading a file

I need the full path. I'm trying to give the functionality of local bookmarks, ie user wants to access c:\MyStuff\Myfile.xls on their local Pc. How do I save/get that value without having to make a tutorial help page on how to cut and paste c:\MyStuff\Myfile.xls .

The below code works in IE7. I understand the point about it being a security issue, but I don't need to save the开发者_JS百科ir choice or even use enctype/multiform, or even submit anything, i just need to get the path they selected.

<html>

<head>
  <title></title>
</head>

<body>
<script language="JavaScript" type="text/javascript">
<!--
vic=0;
document.write('<style> .debug {VISIBILITY: visible; POSITION: absolute; TOP: 500px; z-Index:100; }</style>')
<!---->
</script>

Select a file from directory then save the path<br>
<input type="file" id="dir" value="dir" style="width:0px;" >
<input type="button" value="Save Path" onclick="javascript:SavePath();" >

<script language="JavaScript" type="text/javascript">
<!-- works on ie8 not ff.
function SavePath(){
if (document.getElementById('dir').value==''){
  alert('Select a file from the directory');
  return;
}
Path=document.getElementById('dir').value.substring(0,document.getElementById('dir').value.lastIndexOf('\\'));
alert('variable Path='+Path+'\\ ');
}
//-->
</script>
</body>

</html> 


I need the full path.

You won't get it!

Sorry, there is no way.

(You can't rely on anything useful being in the filename at all: even if the browser includes the whole path, if it ain't a Windows box you're not going to get \\ path-separators. You might get /... or something else completely.)

How do I save/get that value without having to make a tutorial help page on how to cut and paste

You could try encouraging them to drag-and-drop the file icon to a simple text field. In many browsers that will paste in the pathname.

language="JavaScript"

You don't need that.

javascript:

You don't need that.

<!--
    ...
<!---->

You don't need that. (And if you did, the mismatched --s would be invalid.)

<!-- works on ie8 not ff.

Not properly a JavaScript comment.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜