1

Iam trying to upload multiple images and files in my xamarin forms app.I am using Media.plugin and file.picker plugin to get images and files respectively. I used multipart-formdata to upload these files to the web API.I am little confused with the key value parameter of multi-part

Here is the form in which the files to be uploaded.(It is in Restsharp. I am trying to do it in httpclient)

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

How I am fetching image from gallery

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

Uploading Part

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

But this gave me upload error.I know the issue maybe incorrect message body compared to the restsharp portion.How can I change the message body of this multipart form-data into that? Any help is appreciated

Anand
  • 1,866
  • 3
  • 26
  • 49
  • Does this answer your question? [Upload images using multipart/form data](https://stackoverflow.com/questions/60716703/upload-images-using-multipart-form-data) – Lucas Zhang Mar 20 '20 at 06:12
  • @LucasZhang-MSFT No bro – Anand Mar 20 '20 at 06:13
  • @LucasZhang-MSFT Where I am struggling is convert the formdata into the format of restsharp mentioned – Anand Mar 20 '20 at 06:14
  • @LucasZhang-MSFT How to make the messagebody of this multipart content to this strcutre? ("notification_files", "[ { \"name\": \"image.jpg\", \"key\": \"1583307983694\" } ]"); – Anand Mar 20 '20 at 06:36

0 Answers0