Upload files with web browser c#

I am developing a process automation system, where my client's vendor website has a part that needs file upload. All automation is already developed, missing only this part. I've searched several forums, and they all present the solution using SendKeys. This does not work in my case, was there will be more instances of the robot running on the same machine, and will also have humans using that computer.

Found this project in codeproject https://www.codeproject.com/articles/28917/setting-a-file-to-upload-inside-the-webbrowser-com. but, it also did not work for me.

Summarizing

Upload file into a input type file component through the web browser, without using sendkeys.

Example Html

<form id="frmArq" name="frmArq" method="post" enctype="multipart/form-data" action="./outage_upload.php" target="upload_target">
<input type="file" id="arqEvid" name="arqEvid" value="0">
<input type="hidden" id="id_ticket" name="id_ticket" value="7539371">
<input type="submit" value="Enviar"></form>
Author: Diego Moreno, 2017-09-05

1 answers

I believe you can bypass the webbrowser in sending the file.

From what I've seen, the URL outage_upload.php expects a request by POST that sends the file as a parameter. That is, you can send a request directly to that URL, informing the bytes of that file.

I found a similar answer here: https://stackoverflow.com/a/19664927/4713574 and changed it to its context.

Try to do like this:

private System.IO.Stream Upload(string actionUrl, string id_ticket, byte [] file)
{
    HttpContent stringContent = new StringContent(id_ticket);
    HttpContent bytesContent = new ByteArrayContent(file);
    using (var client = new HttpClient())
    using (var formData = new MultipartFormDataContent())
    {
        formData.Add(id_ticket, "id_ticket", "id_ticket");
        formData.Add(file, "arqEvid", "arqEvid");
        var response = client.PostAsync(actionUrl, formData).Result;
        if (!response.IsSuccessStatusCode)
        {
            return null;
        }
        return response.Content.ReadAsStreamAsync().Result;
    }
}

I had no way to test if this is really going to work, and because I'm creating a robot that aims to send these images, I think it can be done that way, coming out of webbrowser.

I hope I helped.

 2
Author: Rovann Linhalis, 2017-10-29 12:28:21