Upload files with web browser c#

Asked

Viewed 529 times

5

I am developing a process automation system, where my client’s supplier website has a part that needs file uploading. 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.

I found this project in the codeproject https://www.codeproject.com/articles/28917/setting-a-file-to-upload-inside-the-webbrowser-com. But it didn’t work for me either.

In short

Upload file to a component input type file via web browser, without using Ndkeys.

Sample 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>
  • You can use Asp.net or the Razor syntax to develop as a web application, it will be easier in many ways, not only for uploading files, applications can be developed with pure language and displayed as a web page, this is just a suggestion, I posted it in response because I don’t have enough reputation to comment on it yet. ps: less than a month on the forum e.e

  • nothing yet ? do you have a page that is online for testing ?

  • It’s an intranet.

1 answer

2


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

From what I saw, the URL outage_upload.php awaits a request by POST that sends the file as a parameter. That is, you can send a request directly to that URL, stating the bytes of that file.

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

Try to do so:

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 whether this will really 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’ve helped.

  • In this case there is a second parameter that I have to submit, which is the id_ticket. How do I include it in the client’s value?

  • I’ll look it up

  • @Diegomoreno see change in response

  • @Diegomoreno all right?

  • 1

    These changes will enter the next sprint. I haven’t had a chance to test. But as you strive to tell a solution, I have considered your answer. So, that I have tested, I put the result here. To serve those next in need.

  • @Diegomoreno is missing 20 hrs reward, if you can test I will be grateful =]

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.