xamarin.forms应用中的多张图片上传问题

时间:2020-03-20 05:26:22

标签: xamarin xamarin.forms

我正在尝试在xamarin表单应用程序中上传多个图像和文件,我正在使用Media.plugin和file.picker插件分别获取图像和文件。我使用multipart-formdata将这些文件上传到Web API。我对multi-part的键值参数有点困惑

这是要上传文件的形式。(在Restsharp中。我正在尝试在httpclient中进行操作)

request.AddParameter("notification_files", "[
  { 
  \"name\": \"image.jpg\",
  \"key\": \"1583307983694\"
}
]");

我如何从图库中获取图像

    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);


**Getting file using file.picker plugin**

    var FilePath = pickedFile.FilePath;
                                    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)
                {
                    byte[] byteArray = Encoding.UTF8.GetBytes(model.Path);
                    MemoryStream stream = new MemoryStream(byteArray);
                    HttpContent fileStreamContent1 = new StreamContent(stream);
                    fileStreamContent1.Headers.ContentDisposition = new
                    ContentDispositionHeaderValue("form-data")
                    {
                        Name = model.FileName,
                        FileName = model.FileName
                    };

                    fileStreamContent1.Headers.ContentType = new
                    MediaTypeHeaderValue("application/octet-stream");
                    multiContent.Add(fileStreamContent1, "notification_files");
                }

但是这给了我一个上传错误。与restsharp部分相比,我知道问题可能是不正确的邮件正文。如何将这种多部分表单数据的邮件正文更改为该内容? 感谢您的帮助

0 个答案:

没有答案