Skip to content

Instantly share code, notes, and snippets.

@walm
Last active October 10, 2017 10:26
Show Gist options
  • Save walm/f5488e536a0ba329fbd0 to your computer and use it in GitHub Desktop.
Save walm/f5488e536a0ba329fbd0 to your computer and use it in GitHub Desktop.
c# async upload file
private static async Task<bool> UploadFile(string filePath, string actionUrl)
{
FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read);
HttpContent filePathContent = new StringContent(filePath);
HttpContent fileStreamContent = new StreamContent(file);
using (var client = new HttpClient())
using (var formData = new MultipartFormDataContent())
{
formData.Add(filePathContent, "filepath");
formData.Add(fileStreamContent, "file", Path.GetFileName(filePath));
HttpResponseMessage response = await client.PostAsync(actionUrl, formData);
if (!response.IsSuccessStatusCode)
{
log.Error("Error:" + filePath + " not uploaded");
return false;
}
else
{
if (_debug) log.Debug("File:" + filePath + " has beed uploaded");
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment