0

I'm trying to upload a file to an MSMVC controller using the using HttpClient as so :

var client = new HttpClient();
var content = new MultipartFormDataContent(----);

var fileContent = new ByteArrayContent(photoBytes);
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue(DispositionTypeNames.Attachment)
{
    FileName = "test.jpg",
};
content.Add(fileContent);
await client.PostAsync("http://192.168.1.80/upload/upload", content);

My controller looks like :

[httpPost]
public XmlResult(HttpPostedFileBase file)
{

}

The controller action fires, I can set a debug point and example the contents of the request. The Request.Boundry is correct as is Request.TotalBytes but the HttpPostedFileBase is null and Request.Files.Count() is 0...

Any advice on what I'm missing would be great.

Richard Adams
  • 591
  • 1
  • 9
  • 23
  • There is no doubt a header youre missing. Have you considered using `WebClient`'s `UploadFile` method? – Simon Whitehead Jul 02 '13 at 09:10
  • Possible duplicate at http://stackoverflow.com/questions/16906711/httpclient-how-to-upload-multiple-files-at-once – Panagiotis Kanavos Jul 02 '13 at 09:16
  • @Simon Whitehead : I looked at the WebClient implementation but I need to send additional form data which WebClient.UpLoadFile doesn't support. – Richard Adams Jul 02 '13 at 15:31
  • @PanagiotisKanavos : I tried the code from your link before posting, the result is the same. Somebody mentioned that using a stream might be better than sending a byte[]? – Richard Adams Jul 02 '13 at 15:32
  • Have you tried using [Fiddler](http://fiddler2.com/) to see what's going on the wire? – Paulo Morgado Jul 02 '13 at 22:04
  • @SimonWhitehead, I've gone back and tested with WebClient.UploadFile and the result is the same. The controller fires the request is the right size but no files in request.files – Richard Adams Jul 03 '13 at 23:53
  • @RichardAdams When building an MVC form, you **must** specify the `enctype` as `multipart/form-data` for it to recognise files. Can you add that header into your post somehow and check if the controller picks it up then? – Simon Whitehead Jul 04 '13 at 00:22

0 Answers0