I am trying to create an api call to my server that will allow me to upload a .csv file.
client side code
string url = "http://testserver/testapi";
string filePath = @"C:\test.csv";
using (WebClient wc = new WebClient())
{
wc.Headers.Add("Content-Type", "text/plain");
byte[] responseArray = wc.UploadFile(url, filePath);
}
server side code
string savePath = testSavePath;
foreach (string f in Request.Files.AllKeys)
{
HttpPostedFileBase file = Request.Files[f];
file.SaveAs(savePath);
}
I am getting an exception on this line byte[] responseArray = wc.UploadFile(url, filePath);
The strange thing is that when I look at Request
I see
ContentType="multipart/form-data; boundary=---------------------8d006b7c2a5452c"
.
Looking at the docs for UploadFile I see that a WebException will be thrown when The Content-type header begins with multipart.
My question is why is contentType getting set to multipart and how do I prevent it from doing so?