Potential Bug in iOS HTTPClient?

Hi I'm attempting to upload a file to an MSMVC controller using the iOS HTTPClient like so :

public async Task UploadPhotoAsync(string fileName, byte[] photoBytes)
{
httpClient = new HttpClient();
try
{
var multipartForm = new MultipartFormDataContent();
var fileContent = new StreamContent(new MemoryStream(photoBytes));
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "file",
FileName = "test.jpg",
};
fileContent.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
multipartForm.Add(fileContent);

        await httpClient.PostAsync("http://192.168.1.80/upload/upload", multipartForm);
}
catch (Exception ex) 
{
        Console.WriteLine(ex.Message);
}

}

The problem seems to be with the data received at the controller end... The request arrives at the Controller but the Request.Files.Count is zero even though the Request.TotalBytes count is correct?

Has anyone else experienced any issues with HTTPClient on iOS?

Posts

  • RichardAdamsRichardAdams USMember

    Hi I'm attempting to upload a file to an MSMVC controller using the iOS HTTPClient like so :

    public async Task UploadPhotoAsync(string fileName, byte[] photoBytes)
    {
        try
        {
            var multipartForm = new MultipartFormDataContent();
            var fileContent = new StreamContent(new MemoryStream(photoBytes));
            fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
            {
                Name = "file",
                FileName = "test.jpg",
            };
            fileContent.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
            multipartForm.Add(fileContent);
            await _httpClient.PostAsync("http://192.168.1.80/upload/upload", multipartForm);
        }
        catch (Exception ex) 
        {
            Console.WriteLine(ex.Message);
        }
    }
    

    The problem seems to be with the data received at the controller end... The request arrives at the Controller but the Request.Files.Count is zero even though the Request.TotalBytes count is correct?

    Has anyone else experienced any issues with HTTPClient on iOS?

  • I'm running into this same problem. The only way I can get the server to recognize the file is by surrounding the value in quotes:

         fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
                {
                    Name = "\"file\"",
                    FileName = "\"test.jpg\"",
                };
    

    I test this by sending from WinPhone. On iOS if I try and surround the values with quotes I get a FormatException.

    From the mono source:

    public static MediaTypeHeaderValue Parse (string input)
            {
                MediaTypeHeaderValue value;
                if (TryParse (input, out value))
                    return value;
    
                throw new FormatException (input);
            }
    

    Any help would be appreciated.

  • So, this works:

    streamContent.Headers.ContentDisposition = ContentDispositionHeaderValue.Parse("form-data");
                                streamContent.Headers.ContentDisposition.Parameters.Add(new NameValueHeaderValue("name", "\"image\""));
                                streamContent.Headers.ContentDisposition.Parameters.Add(new NameValueHeaderValue("filename", "\"image.jpg\""));
    
Sign In or Register to comment.