C# 使用多部分/表单数据上载图像
以我的xamarin形式。我正在尝试使用mulipart-formdata发送多个图像和文件。后端的API团队为我提供了这种结构 正如您所见,有一个名为“notification_files”的参数,它将发送使用我的应用程序中的Media.Plugin和filepicker插件选择的图像和文件。我知道如何以正常的方式发送数据。但是我如何使用xamarin.forms中的httpclient发送这些FormData呢?API团队给了我他们等效的Restsharp代码:C# 使用多部分/表单数据上载图像,c#,xamarin,xamarin.forms,C#,Xamarin,Xamarin.forms,以我的xamarin形式。我正在尝试使用mulipart-formdata发送多个图像和文件。后端的API团队为我提供了这种结构 正如您所见,有一个名为“notification_files”的参数,它将发送使用我的应用程序中的Media.Plugin和filepicker插件选择的图像和文件。我知道如何以正常的方式发送数据。但是我如何使用xamarin.forms中的httpclient发送这些FormData呢?API团队给了我他们等效的Restsharp代码: var client =
var client = new RestClient("{{api_url}}/MYData");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Authorization", "bearer {{token}}");
request.AddHeader("Content-Type", "application/json");
request.AlwaysMultipartFormData = true;
request.AddParameter("ids", " [{\"id\":1,\"person_id\":5}]");
request.AddParameter("title", " Test");
request.AddParameter("description", " Test");
request.AddParameter("send_text_message", " true");
request.AddParameter("text_message", " Test");
request.AddParameter("notification_type"," global");
request.AddParameter("my_files", "[
{
\"name\": \"abc.jpg\",
\"key\": \"1583307983694\"
}
]");
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
如何使用HttpClient编写此代码
我尝试过的
try {
MultipartFormDataContent multiContent = new MultipartFormDataContent();
foreach (SelectedDocumentModel model in SelectedFileData)
{
byte[] byteArray = Encoding.UTF8.GetBytes(model.Path);
MemoryStream stream = new MemoryStream(byteArray);
HttpContent fileStreamContent1 = new StreamContent(stream);
fileStreamContent1.Headers.ContentDisposition = new
System.Net.Http.Headers.ContentDispositionHeaderValue("form-data")
{
Name = model.FileName,
FileName = model.FileName
};
fileStreamContent1.Headers.ContentType = new
System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
multiContent.Add(fileStreamContent1);
}
multiContent.Add(new StringContent(notificationdetails[0]), "title");
multiContent.Add(new StringContent(notificationdetails[1]), "description");
multiContent.Add(new StringContent(notificationdetails[3]), "type");
multiContent.Add(new StringContent(notificationdetails[7]), "send_text_message");
multiContent.Add(new StringContent(notificationdetails[2]), "text_message");
multiContent.Add(new StringContent(notificationdetails[8]), "send_email");
multiContent.Add(new StringContent(notificationdetails[9]), "notification_type");
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("bearer",Settings.AuthToken);
var response = await client.PostAsync(url, multiContent);
var responsestr = response.Content.ReadAsStringAsync().Result;
await DisplayAlert("Result", responsestr.ToString(), "ok");
}
catch (Exception ex)
{
await DisplayAlert("Result", ex.Message.ToString(), "ok");
}
DataManager是我的可观察集合,包含选定的图像和文件
使用media.plugin选择图像并分配到我的可观察收藏中
var Filename = Path.GetFileName(file.Path);
var FilePath = file.Path;
var newList = new SelectedDocumentModel()
{
FileName = Filename,
SelectedImage = imageSource,
IsLoadingVisible = false,
Path = FilePath
};
DataManager.Add(newList);
感谢您的帮助。我就是这样做的
MultipartFormDataContent multiContent = new MultipartFormDataContent();
multiContent.Headers.ContentType.MediaType = "multipart/form-data";
foreach (SelectedDocumentModel model in SelectedFileData)
{
var upfilebytes = File.ReadAllBytes(model.Path);
multiContent.Add(new ByteArrayContent(upfilebytes, 0, upfilebytes.Count()), "notification_files", model.FileName);
}
multiContent.Add(new StringContent(notificationdetails[0]), "title");
multiContent.Add(new StringContent(notificationdetails[1]), "description");
multiContent.Add(new StringContent(notificationdetails[3]), "type");
multiContent.Add(new StringContent(notificationdetails[7]), "send_text_message");
multiContent.Add(new StringContent(notificationdetails[2]), "text_message");
multiContent.Add(new StringContent(notificationdetails[8]), "send_email");
multiContent.Add(new StringContent(notificationdetails[9]), "notification_type");
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("bearer",Settings.AuthToken);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
您是否尝试以字节[]格式发送具有有效键值的图像。在for循环中添加那些imagepath和name。我实现了使用数据与单图像多部分上传;字节[]byteImageFile=ImageFileToByteArray(imagePath);添加(新的ByteArrayContent(byteImageFile,0,byteImageFile.Count()),“您的密钥”,imagename)@Prasanth Bro我没有按照我的理解获得ITA,您想在“通知文件”键中上载这些图像。因此,您可以在-multiContent.Add(newstringcontent(notificationdetails[9]),“notification_type”)之后尝试上面的代码吗;用同样的方法进行试验;添加(新的StringContent(notificationdetails[9]),“通知类型”);字节[]byteImageFile=ImageFileToByteArray(imagePath);添加(新的ByteArrayContent(byteImageFile,0,byteImageFile.Count()),“通知文件”,图像名称);无论如何,您已经有了映像路径及其name@Prasanth兄弟,问题是,我需要上传多张图片。