以编程方式设置type =" file"的值。输入HTML元素? [英] Programmatically set the value of a type="file" input HTML element?

查看:141
本文介绍了以编程方式设置type =" file"的值。输入HTML元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


注意:

下面的答案反映了2009年旧版浏览器的状态。现在您可以在2017年使用JavaScript动态/编程地设置文件输入元素的值。

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)?

我需要做的是以编程方式从使用.NET WebBrowser的应用程序的javascript代码向服务发送POST请求,基本上是一个嵌入式Internet Explorer。此服务要求其中一个字段为文件。

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 :) ).

或许是一种明确创建和发送POST请求而不是动态创建FORM的方法?

Or perhaps a way to explicitly create and send the POST request instead of creating a FORM dynamically?

I需要使用将在我的 WebBrowser (想想IE)中运行的JavaScript代码。这可能吗?

I need to use JavaScript code that will be run in my WebBrowser (think IE). Is this possible?

推荐答案

不幸的是,这是不可能的,因为它会是一个安全问题。对文件输入字段的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.

由于你可以控制浏览器,你可以使用正常的HTTPWebRequest发送文件,但它不能用JavaScript完成。

Since you have control over the browser, you can send the file using a normal HTTPWebRequest, but it can't be done in JavaScript.

这篇关于以编程方式设置type =" file"的值。输入HTML元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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