9

I am using Microsoft Http Client Libraries to make a multipart request from Windows Phone 8 to the server. It contains a String content having json string and a Stream Content having image stream. Now i get the status OK and request hits on the server. but the logs says the server is not able to get the file name of the image.

content.Add(new StreamContent(photoStream), "files", fileName);

where the photoStream is the image stream, "files" is the name of content and file name is the name of the image file.

So the header value must be:

Content-Disposition: form-data; name=files; filename=image123.jpg

but actually it is:

Content-Disposition: form-data; name=files; filename=image123.jpg; filename*=utf-8''image123.jpg

Why it is appending the "; filename*=utf-8''image123.jpg" part. Is it an issue?

Please let me know any reasons/possibility that i am not able to upload image from WP8.

Deeps
  • 557
  • 9
  • 31

3 Answers3

15
using (var content = new MultipartFormDataContent())
{
    content.Add(CreateFileContent(imageStream, fileName, "image/jpeg"));
}

private StreamContent CreateFileContent(Stream stream, string fileName, string contentType)
{
    var fileContent = new StreamContent(stream);
    fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") 
    { 
        Name = "\"files\"", 
        FileName = "\"" + fileName + "\""
    };
    fileContent.Headers.ContentType = new MediaTypeHeaderValue(contentType);            
    return fileContent;
}
Damith
  • 62,401
  • 13
  • 102
  • 153
  • Thank you. This is a problem when connecting to Campfire's API, who strictly reads the 'filename' property and will fail if you specify a UTF8 file name along with the stardard file name. Your answer helped me a lot, thanks! – Richthofen Mar 01 '14 at 16:16
  • Thanks! Really helped. – Mayur Dhingra Apr 06 '17 at 15:01
  • Thanks!!! it took me quite some time to find a solution for this issue, really appreciated for sharing this tip – bartburkhardt Dec 03 '21 at 13:06
0

For me, using HttpStringContent instead of StreamContent, Damith's solution did not work out but at the end I found this one:

var fd = new Windows.Web.Http.HttpMultipartFormDataContent();
var file = new Windows.Web.Http.HttpStringContent(fs);
file.headers.contentType = new Windows.Web.Http.Headers.HttpMediaTypeHeaderValue("application/octet-stream");
fd.add(file);
file.headers.contentDisposition = new Windows.Web.Http.Headers.HttpContentDispositionHeaderValue.parse("form-data; name=\"your_form_name\"; filename=\"your_file_name\"");

Attention: it is abolute essential that you set contentDisposition after adding the file, otherwise the header will be overwritten by "form-data".

Peter
  • 1
  • 1
-3

My simple solution:

HttpContent fileStreamContent = new StreamContent(new FileStream(xmlTmpFile, FileMode.Open));
var formData = new MultipartFormDataContent();
formData.Add(fileStreamContent, "xml", Path.GetFileName(xmlTmpFile));
fileStreamContent.Headers.ContentDisposition.FileNameStar = null;
user3175253
  • 588
  • 4
  • 10