2

I am using loopback Storage component REST API in Xamarin to finish a file uploading job. However, it does not work and does not return any exceptions to me.

Here is my code:

library using: RestSharp.portable

public async Task addFiles(string name, byte[] file)
{
            try
            {
                var client = new RestClient(App.StrongLoopAPI);
                var request = new RestRequest("containers/container1/upload", HttpMethod.Post);
                request.AddHeader("cache-control", "no-cache");
                request.AddHeader("content-type", "multipart/form-data");
                request.AddFile("file", file, name + ".jpg", System.Net.Http.Headers.MediaTypeHeaderValue.Parse("multipart/form-data"));

                var res = await client.Execute(request);
            }
            catch (Exception ex)
            {
                //return null;
            }
}

Does my function have any problems?

T0xicCode
  • 4,583
  • 2
  • 37
  • 50
MMPaul
  • 21
  • 1

1 Answers1

0

You're setting the Content Type (Mime Type) wrong.

AddFile accepts as the last parameter the Content Type (for example image/jpeg for a JPG image), where you're using multipart/form-data.

There are different ways to figure out the Content Type of a file, see here:

Get MIME type from filename extension

This should fix your issue.

Community
  • 1
  • 1
Kevin Goedecke
  • 1,553
  • 2
  • 14
  • 26