Solved

UPLOAD FILE TO Web API USING POST

Posted on 2016-09-16
5
134 Views
Last Modified: 2016-09-19
I need to upload a file to a WebAPI, I'm using this code to do the upload

public void SendFilePost()
{  
    WebRequest request = WebRequest.Create(url);
    request.Method = "POST";
    byte[] byteArray = File.ReadAllBytes(fileName);
    request.ContentType = "multipart/form-data";
    request.ContentLength = byteArray.Length;

    Stream dataStream = request.GetRequestStream();
    dataStream.Write(byteArray, 0, byteArray.Length);
    dataStream.Close();
    WebResponse response = request.GetResponse();
    Console.WriteLine(((HttpWebResponse)response).StatusDescription);
    reader.Close();
    dataStream.Close();
    response.Close();
}
 In the WebAPI I have this code:

public async Task<HttpResponseMessage> Post()
    {
        if (!Request.Content.IsMimeMultipartContent("form-data"))
            return new HttpResponseException(HttpStatusCode.UnsupportedMediaType);

        var streamProvider =
            new MultipartFormDataStreamProvider(".");

        await Request.Content.ReadAsMultipartAsync(streamProvider);

        var fileNames = streamProvider.BodyPartFileNames;

        foreach (var fileName in fileNames.Keys)
            Console.WriteLine(fileName + " --> " + fileNames[fileName]);

        return new HttpResponseMessage(HttpStatusCode.Created);
    }

I'm always receiving this code:

HttpStatusCode.UnsupportedMediaType
0
Comment
Question by:Anderson Meneses
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 25

Expert Comment

by:dgrafx
ID: 41802526
You could use plupload
http://www.plupload.com/
0
 

Author Comment

by:Anderson Meneses
ID: 41802932
It's a good plugin, I will use it in my web projects, but this project is Winforms.
0
 
LVL 25

Expert Comment

by:dgrafx
ID: 41803040
I see ...

I'm not aware that you can't use plupload with winforms
0
 
LVL 29

Accepted Solution

by:
Gautham Janardhan earned 500 total points
ID: 41805154
try this to post

public static async void SendFilePost(string url, string fileName)
        {
            using (var client = new HttpClient())
            using (var content = new MultipartFormDataContent())
            {
                client.BaseAddress = new Uri(url);
                var fileContent1 = new ByteArrayContent(File.ReadAllBytes(fileName));
                fileContent1.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                {
                    FileName = "mydata.txt"
                };
                content.Add(fileContent1, "mydata.txt");

// make sure to change this to ur api 

                var result = client.PostAsync("/api/home", content).Result;

                Console.WriteLine(result.StatusCode);
                Console.ReadLine();
            }
        }

Open in new window

1
 

Author Closing Comment

by:Anderson Meneses
ID: 41805319
Very good. It worked fine!!!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question