如何在C#Windows表单中自动输入上传表单? [英] How to enter a upload form within my C# windows form automatically?

查看:182
本文介绍了如何在C#Windows表单中自动输入上传表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有文件上传对话框的网页.我正在Windows窗体应用程序的WebBrowser控件中查看此页面. 我想自动输入并提交表格.

I have a webpage with a file upload dialog. I'm viewing this page from my WebBrowser control in my Windows Forms application. I'd like to automatically enter and submit the form.

我知道这个问题已经被问过了(很多).我见过很多人,我输了.但是没有一种解决方案对我有用.

I realise this question has been asked before (a lot). I've seen so many of them I lost the count. But none of the solutions have worked for me.

我的网页外观

<form method="post" enctype="multipart/form-data">
   <input id="file" name="file" type="file"/>
   <input type="submit"/>
</form

我的C#应用​​程序看起来像

My C# application looks like

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        webBrowser1.Document.GetElementById("file").Focus();
        SendKeys.Send("C:\\Users\\Merlin Sweden\\Desktop\\extraDirTest\\myImage.jpg{ENTER}");
    }
}

从我尝试过的所有事情中,至少感觉到我正在接近. 无论如何,执行对话框时会打开并输入部分路径(weden \ Desktop \ extraDirTest \ myImage.jpg).我以为可能是某种最大输入长度,我在路径(extraDirTest)中添加了另一个目录,发现那不是问题.

From all the things I've tried this at least felt like I was getting close. Anyway, when executing the dialog opens and part of the path is entered (weden\Desktop\extraDirTest\myImage.jpg). I thought it might be some sort of maximum input length, I've added another directory in the path (extraDirTest) and discovered that that was not the issue.

所以我认为它与空间有关,但我找不到任何替代方法来输入空间.同样,如果我发送的PATH不正确,则对话框完全不会打开.而且,如果我在表单上放置一个文本框并尝试在其中输入文本框,那么一切正常.

So I'm thinking it is related to the space BUT i cannot find any alternative to enter a space. Also if the PATH i send is incorrect the dialog doesn't open at all. And if i place a textbox on my form and try to enter it in there then all works fine.

我不知道发生了什么或应该如何解决.

I don't understand what is happening or how I should fix this.

有人可以帮助我吗?

谢谢

修改 也许值得一提的是,将文件移至"C:\ myImage.jpg"并更改路径无法执行任何操作.

Edit Perhaps it is worth mentioning that moving the file to "C:\myImage.jpg" and changing the path doesn't do anything.

推荐答案

之所以只输入一半,是因为焦点"不会触发对话框. 空间确实如此.因此,仅在空格之后打开对话框,然后在对话框输入后发送任何内容.

The reason only half of it is entered is because the 'focus' does not trigger the dialog. The space does. So the dialog is only opened after the space and whatever is sent after that it entered.

由于某种原因,空格后的字符也被忽略了,可能与计时有关.

For some reason the character right after the space is also left out, probably related to timing.

我已经结束了

SendKeys.Send("  C:\\extraDirTest\\sednasoftwaread.jpg{ENTER}");

此后,您也不能立即提交表单,因为这样会在填写输入内容之前提交表单.睡眠也不是答案,因为对话框一直在睡眠.使用计时器,可以在输入输入后提交表单.

Also afterwards you cannot instantly submit the form because then the form will be submitted before the input is filled. Sleeping also isn't the answer because the dialog sleeps along. Using a timer the form can be submitted after the input has been entered.

每个人都感谢您的帮助!

Everyone thank you for the help!

这篇关于如何在C#Windows表单中自动输入上传表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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