C#:如何设置 mshtml.HTMLInputFileElement 的值
我目前正在开发一个项目,该项目需要能够使用 mshtml.HTMLInputFileElement 在 HTML 文档中设置“文件”类型的输入元素的值。我在做这件事时遇到了很大的困难。
首先我尝试了这个:
IHTMLInputFileElement element = (IHTMLInputFileElement)args[0];
string filename
element.value = newFileName;
但是该值没有设置。然后我在另一个论坛上读到,无法直接设置 value 属性,但可以通过将焦点放在该输入元素上,然后使用 SendKeys 将值发送到文件元素来设置,如下所示:
HTMLInputElement writableFileElement = (HTMLInputElement)element;
writableFileElement.focus();
SendKeys.SendWait(newFileName);
这也失败并引发了 COM异常表明该字段不可写。
有没有办法设置 HTMLInputFileElement 的值字段?
I am currently working on a project that needs to be able to set the value of an input element of type "file" in an HTML document using mshtml.HTMLInputFileElement. I am having a great deal of difficulty doing this.
First I tried this:
IHTMLInputFileElement element = (IHTMLInputFileElement)args[0];
string filename
element.value = newFileName;
But the value was not set. I then read on another forum that the value property could not be set directly, but could be set by giving the focus to that input element and then using SendKeys to send the value to the file element like so:
HTMLInputElement writableFileElement = (HTMLInputElement)element;
writableFileElement.focus();
SendKeys.SendWait(newFileName);
this also failed and threw a COM exception saying that the field was not writable.
Is there any way to set the value field of an HTMLInputFileElement?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,请在您最喜欢的搜索引擎中搜索“浏览器文件输入窃取”以查找原因。
我认为 SendKeys 黑客在 IE8 和 Firefox 2 中已得到修补。
No, search "browser file input stealing" in your favorite search engine for reasons.
The SendKeys hack was patched in IE8 and Firefox 2 I think.