javascript 从浏览按钮获取文件路径 - 不上传文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6288172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
get file path from browse button - do not upload the file
提问by Pre
I have multiple browse buttons on my page. I want to save the path of the files selected by the user and not upload the files themselves. Security is not an issue since the page is on on intranet.
我的页面上有多个浏览按钮。我想保存用户选择的文件的路径,而不是自己上传文件。由于页面位于 Intranet 上,因此安全性不是问题。
Is it possible to get the file path (file name included) so that I can store this value in the database?
是否可以获取文件路径(包括文件名)以便我可以将此值存储在数据库中?
回答by alex
Nope, you can not get it reliably.
不,您无法可靠地获得它。
For example, some browsers will return c:\fakepath\file.jpg
.
例如,某些浏览器会返回c:\fakepath\file.jpg
.
回答by Michael Robinson
Assuming your has an id of upload this should hopefully do the trick:
假设你有一个 id 上传这应该有希望做到这一点:
var fullPath = document.getElementById('upload').value;
if (fullPath) {
var startIndex = (fullPath.indexOf('\') >= 0 ? fullPath.lastIndexOf('\') : fullPath.lastIndexOf('/'));
var filename = fullPath.substring(startIndex);
if (filename.indexOf('\') === 0 || filename.indexOf('/') === 0) {
filename = filename.substring(1);
}
alert(filename);
}
From How to get the file name from a full path using JavaScript?