如何在Firefox 3中从HTML输入表单获取文件路径 [英] How to get the file path from HTML input form in Firefox 3

查看:131
本文介绍了如何在Firefox 3中从HTML输入表单获取文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个简单的HTML表单,包含< input type =file> ,如下所示:

 <形式> 
< label for =attachment>附件:< / label>
< input type =filename =attachmentid =attachment>
< input type =submit>
< / form>在IE7中(也可能是所有着名的浏览器,包括旧的Firefox 2),如果我们提交一个像'// server1 / path / to / file / filename'它正常工作,给出
文件和文件名的完整路径。
$ b

在Firefox 3 ,它只返回文件名,因为他们的新安全功能截断了路径,正如火狐追踪系统( https://bugzilla.mozilla.org/show_bug.cgi?id=143220

我不知道克服这个新功能,因为它会导致我的web应用程序中的所有上传表单停止在Firefox 3上工作。

任何人都可以帮助找到一个单一的解决方案来获取文件在IE7中(也可能是所有着名的浏览器,包括包括Firefox3和IE7在内的所有浏览器的路径)旧的Firefox 2),如果我们提交一个像'// server1 / path / to / fil的文件e /文件名'它正常工作,并给出了文件和文件名的完整路径。

我不知道如何克服这个新功能,因为它导致所有在我的webapp上传表单以停止在Firefox 3上工作。


这里有一个很大的误解。为什么你需要服务器端的 full 文件路径?想象一下,我是客户端,我有一个文件在 C:\path\to\passwords.txt ,我给你完整的文件路径。作为服务器,你将如何获得它的内容?你有一个开放的TCP连接到我的本地磁盘文件系统?当你在不同的服务器机器上把你的webapp带入生产环境时,你测试了文件上传功能吗?



只有当 客户端和服务器在物理上相同的机器上运行,因为您可以访问相同的硬盘文件系统。这只会发生在您本地开发您的网站,因此在同一台机器上运行浏览器(客户端)和网络服务器(服务器)。



完整的文件路径正在发送MSIE和其他古老的网页浏览器是由于一个安全漏洞。 W3 RFC2388 规范从未提及要包含完整的文件路径。只有文件名。 Firefox正在正常工作。



要处理上传的文件,您不需要知道完整的文件路径。在 multipart / form-data 内容感兴趣请求。如RFC2388中所述,将表单改为如下所示:

 < form action =upload-script-urlmethod =postenctype =multipart / form-data> 
< input type =filename =file>
< input type =submit>
< / form>

如何获取服务器端上传文件的内容取决于服务器端编程语言




  • Java / JSP :您可以使用 HttpServletRequest #getPart() Apache Commons FileUpload API 来解析它。你最终应该得到一个带有文件内容的 InputStream ,然后你可以根据自己的口味写入任何 OutputStream 。你可以在这个答案找到一个例子。 Java / JSF :你想使用 < h:inputFile> a>组件或由您正在使用的组件库提供的任何其他文件上传组件。同样在这里,你想获得 InputStream 的风格的文件内容。请参阅此答案举例。

  • :文件内容已经隐式存储在临时磁盘上。你想使用 move_uploaded_file() 功能将其移动到所需的位置。另请参阅 PHP手册


  • ASP.NET :没有详细的回答,因为我不这样做,但是Google为我找到了一些例子: ASP.NET示例 ASP.NET 2.0范例 $ b

    无论何时您想要获取上传文件的文件名部分,都应该从文件名中修剪完整路径。这些信息与你完全无关。另请参阅 Apache Commons FileUpload FAQ条目
    $ b


    为什么FileItem.getName()返回整个路径,而不仅仅是文件名?



    Internet Explorer提供上传文件的完整路径,而不仅仅是基本文件名。由于FileUpload完全提供了客户端(浏览器)提供的内容,因此您可能希望在应用程序中删除此路径信息。


    We have simple HTML form with <input type="file">, like shown below:

    <form>
      <label for="attachment">Attachment:</label>
      <input type="file" name="attachment" id="attachment">
      <input type="submit">
    </form>
    

    In IE7 (and probably all famous browsers, including old Firefox 2), if we submit a file like '//server1/path/to/file/filename' it works properly and gives the full path to the file and the filename.

    In Firefox 3, it returns only 'filename', because of their new 'security feature' to truncate the path, as explained in Firefox bug tracking system (https://bugzilla.mozilla.org/show_bug.cgi?id=143220)

    I have no clue how to overcome this 'new feature' because it causes all upload forms in my webapp to stop working on Firefox 3.

    Can anyone help to find a single solution to get the file path both on Firefox 3 and IE7?

    解决方案

    In IE7 (and probably all famous browsers, including old Firefox 2), if we submit a file like '//server1/path/to/file/filename' it works properly and gives the full path to the file and the filename.

    I have no clue how to overcome this 'new feature' because it causes all upload forms in my webapp to stop working on Firefox 3.

    There's a major misunderstanding here. Why do you ever need the full file path on the server side? Imagine that I am the client and I have a file at C:\path\to\passwords.txt and I give the full file path to you. How would you as being a server ever get its contents? Do you have an open TCP connection to my local disk file system? Did you test the file upload functionality when you've brought your webapp into production on a different server machine?

    It will only work when both the client and server runs at physically the same machine, because you will then have the access to the same hard disk file system. This would only occur when you're locally developing your website and thus both the webbrowser (client) and webserver (server) by coincidence runs at the same machine.

    That the full file path is being sent in MSIE and other ancient webbrowsers is due to a security bug. The W3 and RFC2388 specifications have never mentioned to include the full file path. Only the file name. Firefox is doing its job correctly.

    To handle uploaded files, you should not need to know the full file path. You should rather be interested in the full file contents which the client has already sent to the server in the request body in case of a multipart/form-data request. Change your form to look like the following as stated in RFC2388:

    <form action="upload-script-url" method="post" enctype="multipart/form-data">
        <input type="file" name="file">
        <input type="submit">
    </form>
    

    How to obtain the contents of the uploaded file in the server side depends on the server side programming language you're using.

    • Java/JSP: you'd like to use HttpServletRequest#getPart() or Apache Commons FileUpload API to parse it. You should end up with an InputStream with the file contents which you in turn can write to any OutputStream to your taste. You can find an example in this answer.

    • Java/JSF: you'd like to use <h:inputFile> component or any other file upload component provided by the component library you're using. Also here, you'd like to obtain the file contents in flavor of an InputStream. See this answer for an example.

    • PHP: the file contents is already implicitly stored on the temp disk. You'd like to use move_uploaded_file() function to move it to the desired location. See also PHP manual.

    • ASP.NET: no detailed answer from me since I don't do it, but Google has found some examples for me: ASP.NET example, ASP.NET 2.0 example

    Whenever you want to obtain the file name part of the uploaded file, you should trim the full path off from the file name. This information is namely completely irrelevant to you. Also see for example this Apache Commons FileUpload FAQ entry

    Why does FileItem.getName() return the whole path, and not just the file name?

    Internet Explorer provides the entire path to the uploaded file and not just the base file name. Since FileUpload provides exactly what was supplied by the client (browser), you may want to remove this path information in your application.

    这篇关于如何在Firefox 3中从HTML输入表单获取文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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