js获取文件路径,结果都是C:\fakepath的问题

2025-10-17 02:31:31

1、编写demo.html

demo.html代码如下:

<!DOCTYPE html>


<html>
<head>
<script type="text/script" src="demo.js"
</script>
</head>
<body>
<input id="selectFileInput" type="file" multiple="multiple" list="fileList" />
</body>
</html>

js获取文件路径,结果都是C:\fakepath的问题

2、编写demo.js

demo.js代码如下:

//选择文件


function selectFiles(e){
    var tmpPath = this.value;//获取路径
    if(!tmpPath){
        return null;
    }else if(tmpPath.lastIndexOf('/') > -1){
        return tmpPath.substring('0',tmpPath.lastIndexOf('/'));
    }else if(tmpPath.lastIndexOf('\\') > -1){
        return tmpPath.substring('0',tmpPath.lastIndexOf('\\'));
    }
}

//页面加载事件


window.onload = function(callback){
    document.getElementById("selectFileInput").onchange = selectFiles;
}

js获取文件路径,结果都是C:\fakepath的问题

3、获取路径和原因分析

js获取文件路径为:“C:\fakepath”;

原因分析:window系统为了安全考虑,不让从客户端获取文件路径,统一使用C:\fakepath代替。

js获取文件路径,结果都是C:\fakepath的问题

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢