Upload files with web browser C #

5

I'm developing a process automation system, where my client's vendor site has a part that needs to upload files. All automation is already developed, only missing this part. I have already researched in several forums, and everyone presents the solution using SendKeys. This does not work in my case, there were more instances of the robot running on the same machine, and also humans using that computer.

I found this project on the codeproject link . But it also did not work for me.

Summarizing

Upload file to 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>
    
asked by anonymous 05.09.2017 / 16:42

1 answer

2

I believe you can get around the webbrowser in sending the file.

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

  

I found a similar answer here: link and changed it to its context.

Try to do 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 of testing if this really will work, and because I'm creating a robot that aims to send those images, I think it can be done that way, by leaving the webbrowser.

I hope I have helped.

    
28.10.2017 / 04:10