javascript 如何在javascript中使用输入类型文件获取文件的完整路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4244256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get full path of file using input type file in javascript?
提问by Sonal Patil
I want to implement an attachment feature for email, like Gmail has, using jquery+javascript.
我想使用 jquery+javascript 为电子邮件实现附件功能,就像 Gmail 一样。
So I use input type file to select the file - which gives only the name of file, not the full path of file where it is located. I want the full path of file so that I convert file into bytes then send ajax request to server with bytes of file. I used this:
所以我使用输入类型文件来选择文件 - 它只提供文件名,而不是文件所在的文件的完整路径。我想要文件的完整路径,以便我将文件转换为字节,然后将 ajax 请求发送到带有文件字节的服务器。我用过这个:
<input type="file id="fileUpload">
var filePath=$('#fileUpload').val();
but the filePathvar 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.
但filePathvar 仅包含所选文件的名称,而不包含完整路径。我在网上搜索过它,但出于安全原因,浏览器(FF,chrome)似乎只给出了文件名。
Thank you.
谢谢你。
回答by Adeel Ansari
You don't need it. Believe me.
你不需要它。相信我。
Your best bet, Javaranch: File Upload How-to.
您最好的选择,Javaranch:文件上传方法。
回答by jwueller
That is impossible. If this was possible, i could access any file on your hard drive by manipulating your upload field. Why would you need it, anyway?
那是不可能的。如果这是可能的,我可以通过操作您的上传字段来访问您硬盘上的任何文件。无论如何,你为什么需要它?
回答by Piskvor left the building
As you already noted, many browsers just won't allow you to do this. That is 1) a Good Thing (security and whatnot), 2) the Correct Answer (there's no way to force them to give the full path, short of some exploit). I'm sorry that you don't like it, but that is unlikely to make the answer different.
正如您已经注意到的,许多浏览器都不允许您这样做。那是 1)一件好事(安全性和诸如此类的东西),2)正确答案(没有办法强迫他们提供完整的路径,除了一些漏洞利用)。很抱歉您不喜欢它,但这不太可能使答案有所不同。
On a more positive note, it might be possible using some Flash or Java applet to access local files (e.g. Uploadify), but then the user needs to permit its privilege elevation.
更积极的一点是,可能可以使用某些 Flash 或 Java 小程序来访问本地文件(例如 Uploadify),但是用户需要允许其权限提升。
Edit: I checked what GMail does, and you probably want something completely different from your question: it's using a cleverly disguised iframe(no borders, same background, looks like part of the page), which contains a small form for file upload. Simplified:
编辑:我检查了 GMail 的作用,你可能想要一些与你的问题完全不同的东西:它使用了一个巧妙的伪装iframe(没有边框,背景相同,看起来像页面的一部分),其中包含一个用于文件上传的小表单。简化:
<form id="main_mail_form">
<input type="hidden" name="compose_mail_identifier" value="id_beef-cafe">
<!-- buttons and previous form fields - subject, to, cc -->
<iframe src="attachments?for_message=id_beef-cafe">
<!-- note that we're passing ^^^^^^^^^^^^ the same identifier here -->
</iframe>
<!-- other form fields and buttons -->
</form>
Inside the iframe:
iframe 内部:
<form id="attachment_mail_form">
<input type="hidden" name="compose_mail_identifier" value="id_beef-cafe">
<!-- ^^ note that this has the same value as the main form -->
<input type="file">
<!-- other form elements -->
</form>
When you add an attachment to the mail, you are only submitting the form inside the iframe. Google seems to match the attachments with the e-mails using some per-message-unique identifier (named compose_mail_identifierin the example). That way, the attachments and the message are handled by different forms, and only merged into an e-mail when you click "Send".
当您向邮件添加附件时,您只是在 iframe 内提交表单。谷歌似乎使用一些每条消息的唯一标识符(compose_mail_identifier在示例中命名)将附件与电子邮件匹配。这样,附件和消息由不同的表单处理,并且只有在您单击“发送”时才合并到电子邮件中。
回答by Simon H
You won't be able to access the file in such a fashion as it breaks basic security rules that your browser enforces. A trick that I like to use is to create an iframe with no borders that houses an upload form. You should then be able to upload the file and post back to the parent window with the name of the file and some sort of id if you decide to insert a record of the attachment into a database. I use the iframe because it mimics the sort of "AJAX" like behaviour that you would like to achieve in modern web applications.
您将无法以这种方式访问该文件,因为它违反了浏览器强制执行的基本安全规则。我喜欢使用的一个技巧是创建一个没有边框的 iframe 来容纳一个上传表单。如果您决定将附件的记录插入到数据库中,那么您应该能够上传文件并使用文件名和某种 id 回发到父窗口。我使用 iframe 是因为它模仿了您希望在现代 Web 应用程序中实现的类似“AJAX”的行为。
I created a similar email attachment solution a couple of days ago. It's not as simple as you would like it to be, so yeah the short answer is that you are going to have to put a bit more work and thought into it.
几天前我创建了一个类似的电子邮件附件解决方案。这并不像您希望的那么简单,所以简短的回答是您将不得不投入更多的工作和思考。

