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.
我在Xamarin中使用环回存储组件REST API来完成文件上载作业。但是,它不起作用,也不会向我返回任何例外情况。
Here is my code:
这是我的代码:
library using: RestSharp.portable
库使用: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?
我的功能有问题吗?
1 个解决方案
#1
0
You're setting the Content Type (Mime Type) wrong.
您正在设置内容类型(Mime类型)错误。
AddFile
accepts as the last parameter the Content Type (for example image/jpeg
for a JPG image), where you're using multipart/form-data
.
AddFile接受内容类型的最后一个参数(例如JPG图像的image / jpeg),其中您使用的是multipart / form-data。
There are different ways to figure out the Content Type of a file, see here:
有不同的方法可以找出文件的内容类型,请参见此处:
Get MIME type from filename extension
从文件扩展名获取MIME类型
This should fix your issue.
这应该可以解决您的问题。
#1
0
You're setting the Content Type (Mime Type) wrong.
您正在设置内容类型(Mime类型)错误。
AddFile
accepts as the last parameter the Content Type (for example image/jpeg
for a JPG image), where you're using multipart/form-data
.
AddFile接受内容类型的最后一个参数(例如JPG图像的image / jpeg),其中您使用的是multipart / form-data。
There are different ways to figure out the Content Type of a file, see here:
有不同的方法可以找出文件的内容类型,请参见此处:
Get MIME type from filename extension
从文件扩展名获取MIME类型
This should fix your issue.
这应该可以解决您的问题。