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

查看:28
本文介绍了以编程方式设置 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?

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

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天全站免登陆