如何在使用<input type=‘file’> Select 文件时获取文件的完整路径

<input type="file" id="fileUpload">
<script type="text/javascript">
function getFilePath(){
     $('input[type=file]').change(function () {
         var filePath=$('#fileUpload').val(); 
     });
}
</script>

but the filePath var contains only name of selected file, not the full path.
I searched it on net, but it seems that for security reasons browsers (FF,chrome) just give name of file.
Is there any other way to get full path of selected file?

推荐答案

出于安全原因,浏览器不允许这样做,即浏览器中的JavaScript无法访问文件系统,但是使用HTML5文件API,只有Firefox提供mozFullPath属性,但如果试图获取该值,它将返回一个空字符串:

$('input[type=file]').change(function () {
    console.log(this.files[0].mozFullPath);
});

https://jsfiddle.net/SCK5A/

所以不要浪费你的时间.

编辑:如果需要读取文件的路径,可以使用FileReader API.下面是一个关于SO的相关问题:Preview an image before it is uploaded.

Javascript相关问答推荐

试图为每支球队生成类似于2024/25年欧洲足联冠军联赛瑞士系统格式的独特比赛配对

调用SEARCH函数后,程序不会结束

如何使用Paged.js仅渲染特定页面

*ngFor和@代表输入decorator 和选角闭合

在贝塞尔曲线的直线上找不到交叉点:(使用@Pomax的bezier.js)

如何在使用fast-xml-parser构建ML时包括属性值?

TypScript界面中的Infer React子props

通过在页面上滚动来移动滚动条

微软Edge编辑和重新发送未显示""

在我的html表单中的用户输入没有被传送到我的google表单中

如何从网站www.example.com获取表与Cheerio谷歌应用程序脚本

Javascript json定制

还原器未正确更新状态

Websocket错误—有一个或多个保留位开启:reserved1 = 1,reserved2 = 0,reserved3 = 0

如何通过将实例方法的名称传递给具有正确类型的参数的继承方法来调用实例方法?

在使用REACT更改了CSS类之后,无法更改CSS样式

VSCode中出现随机行

将异步回调转换为异步生成器模式

为列表中的项目设置动画

无法检索与Puppeteer的蒸汽游戏的Bundle 包价格