Last active
October 10, 2017 10:26
-
-
Save walm/f5488e536a0ba329fbd0 to your computer and use it in GitHub Desktop.
c# async upload file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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