Search code examples
c#xamarinpostuploadiformfile

Uploading file to server requires IFileForm object as POST parameter


First, let me say I'm not that experienced with networking.

I need to upload a file to a server, and the Swagger docs says it requires a POST with a few parameters:

File : object
UserName : string

Also on the Swagger docs page, it provides the Model

IFormFile {
   contentType string
   contentDisposition string
   headers { <*>: [string] }
   length integer($int64)
   name string
   fileName string
}

I am making a Xamarin.Forms app (UWP and iOS), and notice the IFormFile and FormFile definitions are in the AspNetCore namespace and I don't seem to have access to that.

So I made a mock FormFile class

public class FormFile
{
    public string contentType { get; set; }
    public string contentDisposition { get; set; }
    public List<string> headers { get; set; }
    public long length { get; set; }
    public string name { get; set; }
    public string fileName { get; set; }
}

I'm trying to upload with:

public async Task UploadFile1(string filePath, string userName, string authToken)
{
    var fileInfo = new FileInfo(filePath);
    var fileName = fileInfo.Name;

    var formFile = new FormFile
    {
        contentType = "multipart/form-data",
        contentDisposition = "form-data",
        headers = new List<string>(),
        length = fileInfo.Length,
        name = "files",
        fileName = fileName
    };

    using (var client = new HttpClient())
    {
        client.MaxResponseContentBufferSize = 256000;
        client.BaseAddress = new Uri("https://my.url/");
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("multipart/form-data"));
        client.DefaultRequestHeaders.Add("x-auth-token", authToken);
        using (var content = new MultipartFormDataContent("---upload"))
        {
            content.Add(new FormUrlEncodedContent(new []
            {
                new KeyValuePair<string, string>("File", JsonConvert.SerializeObject(formFile)),
                new KeyValuePair<string, string>("UserName", userName),
            }));
            content.Headers.ContentType.MediaType = "multipart/form-data";
            var fileStream = File.OpenRead(filePath);
            content.Add(new StreamContent(fileStream), fileName, fileName);
            using (var response = await client.PostAsync("api/datasets", content))
            {
                string received;
                if (response.IsSuccessStatusCode)
                {
                    received = await response.Content.ReadAsStringAsync();
                }
                else
                {
                    received = response.ToString();
                }
            }
        }
    }
}

The response I'm getting is

StatusCode: 422, ReasonPhrase: 'Unprocessable Entity', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Server: Kestrel
  Date: Wed, 24 Oct 2018 14:27:10 GMT
  X-Powered-By: ASP.NET
  Access-Control-Allow-Origin: *
  Content-Length: 0
}

I'm completely at a loss as to how to perform an upload to this server.


Solution

  • IFormData is the way to handle a "normal" file upload. So I guess you should

    • Remove your FormFile class, do not send anything json encoded.
    • Do a "normal" file upload => send the file content itself as "File"

    something like this

    content.Add(new StreamContent(new MemoryStream(image)), "File", "upload.jpg");
    

    From: C# HttpClient 4.5 multipart/form-data upload

    Please note that I didn't spend much time looking for a file upload example (there should be enough available).