Mahendran V M
Mahendran V M

Reputation: 3496

How to write code to post the file in application/form-data in C# console?

I'm using Post Man in windows to post the file in application/form-data into web url like below.,

  http://{host}:{port}/file

File in form-data is..,

  file "C:/Temp/file.txt"

In postMan it's worked .

But i wants to write code for perform this in C# console application.

I am new to this.So please anyone give any approach to write code to process file as part of url in Post Method{application/form-data} in C#. how-to-fill-forms-and-submit-with-webclient-in-c-sharp

I have checked link attached .

It only have code to pass the "application/x-www-form-urlencoded" only.

But i need code for "application/form-data".

Note: I have tried that below code in that link it shows 415 Unsupported media Type error only.

var encoding=new ASCIIEncoding();
var postData="C:/test.csv";
byte[]  data = encoding.GetBytes(postData);

var myRequest =
  (HttpWebRequest)WebRequest.Create("http://localhost/MyIdentity/Default.aspx");
myRequest.Method = "POST";
myRequest.ContentType="application/form-data";
myRequest.ContentLength = data.Length;
var newStream=myRequest.GetRequestStream();
newStream.Write(data,0,data.Length);
newStream.Close();

var response = myRequest.GetResponse();
var responseStream = response.GetResponseStream();
var responseReader = new StreamReader(responseStream);
var result = responseReader.ReadToEnd();

responseReader.Close();
response.Close();

Upvotes: 1

Views: 2103

Answers (2)

Mahendran V M
Mahendran V M

Reputation: 3496

This code only works fine for "multipart/form-data"

//Convert each of the three inputs into HttpContent objects
                byte[] fileBytes = System.IO.File.ReadAllBytes(filePath);

                HttpContent bytesContent = new ByteArrayContent(fileBytes);

                // Submit the form using HttpClient and 
                // create form data as Multipart (enctype="multipart/form-data")

                using (var client = new System.Net.Http.HttpClient())
                using (var formData = new MultipartFormDataContent())
                {
                    // <input type="text" name="filename" />
                    formData.Add(bytesContent, "filename", Path.GetFileName(filePath));

                    // Actually invoke the request to the server

                    // equivalent to (action="{url}" method="post")
                    var response = client.PostAsync(url, formData).Result;

                    // equivalent of pressing the submit button on the form
                    if (!response.IsSuccessStatusCode)
                    {
                        return null;
                    }
                    return response.Content.ReadAsStreamAsync().Result;
                }

Upvotes: 3

Azaz ul Haq
Azaz ul Haq

Reputation: 1747

I believe you should try multipart form-data instead of application/form-data. I've successfully posted a PowerPoint file to a ASP.NET MVC Controller for processing of the file on server. Here is the link showing how to upload file using multipart form content type.

Upvotes: 1

Related Questions