文件上传和C:\ fakepath \

I was recently working on a project that required providing an AJAX uploading widget to users. I decided to use Dojo's dojox.form.FileInput widget so the "Upload" button would look just like every other button within the web application. Everything worked great until I tested the widget in Chrome and found that the value of the input node was being set to C:\fakepath\{Original File Name}. I then checked Internet Explorer and Safari; both of them were prepending "C:\fakepath" to the file name. WTF?!

我最近正在从事一个需要向用户提供AJAX上传小部件的项目。 我决定使用Dojo的dojox.form.FileInput小部件,以便“上载”按钮看起来像Web应用程序中的所有其他按钮一样。 一切正常,直到我在Chrome中测试了小部件并发现输入节点的值已设置为C:\fakepath \ {Original File Name}。 然后,我检查了Internet Explorer和Safari。 他们两个都在文件名前加上“ C:\fakepath ”。 WTF ?!

After doing some research, I found this blog post, which explained:

经过研究,我发现了这篇博客文章 ,其中解释了:

According to the specifications of HTML5, a file upload control should not reveal the real local path to the file you have selected, if you manipulate its value string with JavaScript. Instead, the string that is returned by the script, which handles the file information is C:\fakepath.

根据HTML5的规范,如果您使用JavaScript操作其值字符串,则文件上传控件不应显示所选文件的真实本地路径。 而是由脚本返回的用于处理文件信息的字符串是C:\fakepath

This requirement is already implemented in Internet Explorer 8 - the real path to the file will be shown only if the page that contains the control is added to the trusted sites collection of the browser.

Internet Explorer 8中已经实现了此要求-仅当包含控件的页面添加到浏览器的受信任网站集时,才会显示文件的真实路径。

That made sense; essentially the browser is feeding that lame C:\fakepath\ text in. Luckily all I needed to do was fix the issue by doing a simple string replace call:

那是有道理的; 本质上,浏览器正在输入C:\fakepath \文本。幸运的是,我需要做的只是通过执行简单的字符串替换调用来解决此问题:


// Change the node's value by removing the fake path
inputNode.value = fileInput.value.replace("C:\\fakepath\\", "");


Whew -- dodged a bullet there. Just wanted to post this for everyone in case you run into it in the future.

ew-躲在那儿。 只是想将其发布给所有人,以防将来您遇到它。

翻译自: https://davidwalsh.name/fakepath

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值