Skip to content

Instantly share code, notes, and snippets.

@felipebossolani
Created June 27, 2019 10:15
Show Gist options
  • Save felipebossolani/8e37f48003399fb75b5707545b5c94e6 to your computer and use it in GitHub Desktop.
Save felipebossolani/8e37f48003399fb75b5707545b5c94e6 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
namespace AspNetCoreMultipleUploadAPI.ConsoleApp
{
public class UploadRestClientModel
{
private string BASE_URL = "https://localhost:44336/api/file/";
public Task<HttpResponseMessage> Upload(List<FileInfo> fileInfos)
{
try
{
var httpClient = new HttpClient();
var multipartFormDataContent = new MultipartFormDataContent();
httpClient.BaseAddress = new Uri(BASE_URL);
foreach (var fileInfo in fileInfos)
{
var fileContent = new ByteArrayContent(File.ReadAllBytes(fileInfo.FullName));
multipartFormDataContent.Add(fileContent, "files", fileInfo.Name);
}
return httpClient.PostAsync("upload", multipartFormDataContent);
}
catch
{
return null;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment