0

I've been googling this for a little while now. I've seen other stack over flow questions but none of them had the appropriate answer for this problem.

So, basically: I'm trying to reconstruct a form using C# to submit data.

My code is this:

        var documentContent = new MultipartFormDataContent();
        documentContent.Add(new StringContent("someTag"), "tags[]");
        documentContent.Add(new StringContent("https://mywebsitehere.example"), "website");
        documentContent.Add(new ByteArrayContent(File.ReadAllBytes("myFile.gif")), "animation");
        var response = client.PostAsync("https://mywebsitehere.example", documentContent).Result;
        var responseContent = response.Content.ReadAsStringAsync().Result;
        Print(responseContent);

So for the top one, I'm trying to set data for this:

 <div class="form-group">
    <label for="tags">Tags</label>
    <select  class="form-control select2-multiple" id="tags" name="tags[]" multiple="multiple">

The middle one being:

 <div class="form-group">
    <label for="website">Website</label>
    <input type="text" class="form-control" id="website" name="website" placeholder="Optional">
  </div>

And lastly:

<div class="form-group">
    <label for="animation">Animation (Optional)</label>
    <input type="file" name="animation" id="animation">
    <p class="help-block">Upload animation</p>
  </div>

I'm really not sure how to make this possible, but if someone could direct me in the right direction to submit this data using C# that would be awesome!!

ADyson
  • 44,946
  • 12
  • 41
  • 55
Velcer
  • 33
  • 5
  • Possible duplicate of [How to upload file to server with HTTP POST multipart/form-data?](https://stackoverflow.com/questions/19954287/how-to-upload-file-to-server-with-http-post-multipart-form-data) – ADyson Sep 13 '19 at 08:04
  • I've seen that. I've been unable to incorporate that. – Velcer Sep 13 '19 at 22:03
  • unable how, exactly? What did you try? What went wrong? please post your attempt. It should in theory be possible to use that information to make a successful request. – ADyson Sep 13 '19 at 22:04

1 Answers1

-1

Lots of different ways to solve this problem with C#. You could create a MVC application and handle the POST with a Controller Action. See tutorial here: https://docs.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api

Or you could use another approach and use Razor Pages. https://www.learnrazorpages.com/razor-pages/forms

These are just 2 examples but there are many different "frameworks" on top of .Net / .Net Core.

A question to ask before you pick one would be:

What platform am I targeting?

If just Windows, using the "Full Framework" can provide some nice productivity wins. The tooling is mature and lots of docs and articles. If you want to run on anything other than Windows then .NetCore is your friend.

Microsoft's docs site https://docs.microsoft.com/en-us/aspnet/ contains the docs and tutorials for both Core and Full Framework approaches.

wickdninja
  • 769
  • 5
  • 15
  • No, the OP is asking how to **send** the data using C#. These solutions are all for how to **receive** it – ADyson Sep 13 '19 at 06:35