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