Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save geiltonxavier/f49b6ef15f7fe9a4ef9b4dc71162d3d3 to your computer and use it in GitHub Desktop.
Save geiltonxavier/f49b6ef15f7fe9a4ef9b4dc71162d3d3 to your computer and use it in GitHub Desktop.
Upload();
// https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-requests
// https://www.aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/
void Upload()
{
Console.WriteLine("init - upload file");
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "API Service");
client.DefaultRequestHeaders.TryAddWithoutValidation("accept", "*/*");
using (var content = new MultipartFormDataContent())
{
var path = @"you_file_path";
string fileName = Path.GetFileName(path);
Console.WriteLine($"upload file name {fileName}");
FileStream fs = File.OpenRead(path);
var streamContent = new StreamContent(fs);
content.Add(streamContent, "file", Path.GetFileName(path));
Console.WriteLine($"Post file {fileName} Async");
var url = "service_upload_url";
Task<HttpResponseMessage> message = client.PostAsync(url, content);
Console.WriteLine($" PostAsync");
var input = message.Result.Content.ReadAsStringAsync();
Console.WriteLine($"Result: {input.Result}");
Console.Read();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment