注意:
下面的答案反映了 2009 年旧版浏览器的状态。现在,您实际上可以在 2017 年使用 JavaScript 动态/编程地设置文件输入元素的值。
有关详细信息以及演示,请参阅此问题中的答案:
如何以编程方式设置文件输入值(即:当拖放文件时)?
我需要做的是以编程方式从使用 .NET WebBrowser
的应用程序中的 javascript 代码向服务发送 POST 请求,该应用程序基本上是一个嵌入式互联网浏览器。该服务要求其中一个字段是“文件”。
那么,考虑到我有文件的内容,例如在字节数组中(而不是路径),有什么方法可以设置表单中“文件”输入的值文件的;我不想从这里的用户那里窃取文件:))。
或者也许是一种显式创建和发送 POST 请求而不是动态创建 FORM 的方法?
我需要使用将在我的 WebBrowser
(例如 IE)中运行的 JavaScript 代码。这可能吗?
Note:
The answer(s) below reflect the state of legacy browsers in 2009. Now you can actually set the value of the file input element dynamically/programatically using JavaScript in 2017.
See the answer in this question for details as well as a demo:
How to set file input value programatically (i.e.: when drag-dropping files)?
What I need to do is to programmatically send a POST request to a service from javascript code from an application that uses a .NET WebBrowser
, which is basically an embedded Internet Explorer. This service requires one of the fields to be a "file".
So either, is there any way to set the value of a "file" input in a form, given that I have the content of the file, such as in an array of bytes (and not the path of the file; I'm not trying to steal files from users here :) ).
Or perhaps a way to explicitly create and send the POST request instead of creating a FORM dynamically?
I need to use JavaScript code that will be run in my WebBrowser
(think IE). Is this possible?
发布评论
评论(1)
不幸的是,这是不可能的,因为这将是一个安全问题。 Javascript 对文件输入字段的控制非常有限。否则人们可以利用它来窃取用户计算机上的文件。
由于您可以控制浏览器,因此您可以使用普通的 HTTPWebRequest 发送文件,但不能在 JavaScript 中完成。
Unfortunately this is not possible as it would be a security issue. Javascript control over file input fields is very limited. Otherwise people could use this to steal files from user's computers.
Since you have control over the browser, you can send the file using a normal HTTPWebRequest, but it can't be done in JavaScript.