当用户通过< input type =“file”选择文件时,如何获取文件名/&GT ;? [英] How to get file name when user select a file via <input type="file" />?

查看:563
本文介绍了当用户通过< input type =“file”选择文件时,如何获取文件名/&GT ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前看过类似的问题,最终没有解决方案,因为安全的原因。

I've seen similar questions before,which ends up with no solution,because of security reasons.

但是今天我看到hostmonster已经成功实现了,当我

But today I see hostmonster has successfully implemented this,when I open a ticket and attach a file in their backend.

它兼具Firefox和IE(版本8)。

It works both with firefox and IE(version 8 to be exact).

我已经验证它是客户端脚本,没有请求发送(与firebug)。

I've verified it's exactly client side scripting,no request is sent(with firebug).

现在,我们可以重新考虑这个问题吗?

Now,can we re-consider this question?

推荐答案

您可以获取文件名,但无法获取完整的客户端文件系统路径

You can get the file name, but you cannot get the full client file-system path.

尝试访问文件属性事件。

Try to access to the value attribute of your file input on the change event.

大多数浏览器将只提供文件名,但有一些例外,如IE8,它会给你一个假的路径,如:C:\fakepath\myfile.ext和旧版本( IE <= 6 )实际上会给你完整的客户端文件系统路径(由于其缺乏安全性)。

Most browsers will give you only the file name, but there are exceptions like IE8 which will give you a fake path like: "C:\fakepath\myfile.ext" and older versions (IE <= 6) which actually will give you the full client file-system path (due its lack of security).

document.getElementById('fileInput').onchange = function () {
  alert('Selected file: ' + this.value);
};

这篇关于当用户通过&lt; input type =“file”选择文件时,如何获取文件名/&GT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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