较旧的浏览器过去允许不受限制地访问完整路径,因此这并非不可能,但出于安全考虑,您最好的答案将是一种解决方法。
IE浏览器
HTA Application
如果您在本地工作,一种选择是可以将页面作为 HTML 应用程序运行。遗憾的是,它使用 Internet Explorer 作为引擎。但如果您可以使用 HTA,那么这将满足您的需求:
<!--test.hta-->
<HTML>
<HEAD>
<HTA:APPLICATION ID="testFile" BORDER="thick" BORDERSTYLE="complex"/>
<TITLE>HTA - Test file</TITLE>
</HEAD>
<BODY>
<input type="file" onchange="alert(this.value)">
</BODY>
</HTML>
值得信赖的网站
更好的选择是使用 Internet Explorer,然后将您的页面添加到 Internet Explorer 的受信任站点。那么你的解决方案就很简单:
<input type="file" id="fileUpload" onchange="alert(this.value)">
以下是将站点添加到受信任站点的方法:
自定义安全级别
您还可以为 Internet Explorer 全局启用此行为:
Firefox
Firefox does not appear to have support for grabbing the full URL. But as mentioned
here there does seem to exist a "mozFullPath" property:
https://developer.mozilla.org/en-US/docs/Web/API/File/mozFullPath
我在浏览器中尝试了一下,它似乎是一个不存在的属性。我在任何地方都找不到有关如何利用此属性的任何文档。但它是一个需要密切关注的属性,以防它变得有用。
HTML5
In HTML5, you can write
this.files[0]
to refer to the File object. Properties include "name" and "lastModifiedDate", "size", and "type" as mentioned here:
https://developer.mozilla.org/en-US/docs/Web/API/File
在 HTML5 中,您实际上可以使用 blob 并从所选文件创建对象 url 并显示预览。这可以通过以下方式完成URL.createObjectURL(...)然后创建图像并设置其src
到生成的临时 url。看这把小提琴.(归功于这篇文章)
最后,您可能会非常喜欢:
https://blueimp.github.io/jQuery-File-Upload/