I have the following function
public bool UploadFirmware(string ipAddress)
{
Uri url = new Uri($"https://{ipAddress}/upload.html");
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string selectedFileName = openFileDialog1.FileName;
if (selectedFileName == string.Empty)
return false;
else
{
using (var webClient = new WebClient())
{
webClient.UploadFileAsync(url, "POST", selectedFileName);
webClient.UploadFileCompleted += WebClient_UploadFileCompleted;
}
}
}
return false;
}
This function works correctly and it is able to upload the file to the destination. However, I wanted to change the WebClient with and HttpClient as I want to upload files to multiple destinations at the same time without making a new WebClient for each request. My HttpClient is as follows:
public async Task<bool> UploadFirmware(string[] ipAddresses)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string selectedFileName = openFileDialog1.FileName;
if (selectedFileName == string.Empty)
{
return false;
}
else
{
var multiForm = new MultipartFormDataContent();
var array = File.ReadAllBytes(selectedFileName);
var fileContent = new ByteArrayContent(array);
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data");
multiForm.Add(fileContent, "files", Path.GetFileName(selectedFileName));
var url = $"https://{ipAddresses[0]}/upload.html";
var response = await httpClient.PostAsync(url, multiForm);
var test = response.Content;
return true;
}
}
return false;
}
With my current implementation the response is 400, Bad request, leading me to think that I have not created the request correctly as of yet. My request is if you could point me to the right direction, what am I doing wrong? Is HttpClient needed? It might be that creating multiple WebClient would not pose a problem.
PS: The files that I am sending are of type .fw and currently I am trying to get one response to get through. After I am able to do a single request correctly I will be using Tasks.