如何在更改<输入类型=‘文件’>时获取所选文件的完整路径;使用 javascript、jquery-ajax? [英] How to get full path of selected file on change of &lt;input type=‘file’&gt; using javascript, jquery-ajax?

查看:22
本文介绍了如何在更改<输入类型=‘文件’>时获取所选文件的完整路径;使用 javascript、jquery-ajax?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在使用

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

但文件路径变量包含所选文件的only name,而不是完整路径.
我在网上搜索过它,但出于安全原因,浏览器(FF、chrome)似乎只给出了文件名.
有没有其他方法可以获得所选文件的完整路径?

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 属性,但如果您尝试获取该值,它将返回一个空字符串:

For security reasons browsers do not allow this, i.e. JavaScript in browser has no access to the File System, however using HTML5 File API, only Firefox provides a mozFullPath property, but if you try to get the value it returns an empty string:

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

https://jsfiddle.net/SCK5A/

所以不要浪费你的时间.

So don't waste your time.

如果您需要文件的路径来读取文件,您可以使用 FileReader API 代替.这是一个关于 SO 的相关问题:在上传之前预览图像.

edit: If you need the file's path for reading a file you can use the FileReader API instead. Here is a related question on SO: Preview an image before it is uploaded.

这篇关于如何在更改<输入类型=‘文件’>时获取所选文件的完整路径;使用 javascript、jquery-ajax?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆