Skip to content

Instantly share code, notes, and snippets.

@angusbreno
Last active January 19, 2018 12:55
Show Gist options
  • Save angusbreno/aeb7f8adfda38db46ae0fc010891687f to your computer and use it in GitHub Desktop.
Save angusbreno/aeb7f8adfda38db46ae0fc010891687f to your computer and use it in GitHub Desktop.
UploadFile WebAPI - Client
using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
namespace UploadImageTest
{
public class BlobHelper
{
public static async Task<IList<string>> UploadPhoto(params FileUploadCommand[] fileCommands)
{
try
{
var content = new MultipartFormDataContent();
foreach (var fileCommand in fileCommands)
{
var fileContent = new ByteArrayContent(fileCommand.Bytes);
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = Guid.NewGuid().ToString() + fileCommand.Extension
};
content.Add(fileContent);
}
using (var client = new HttpClient())
{
var response = await client.PostAsync("http://localhost:50461/core/api/Test/UploadFile", content);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsAsync<IList<string>>(); //retorna list de urls do blob
}
}
catch (Exception ex)
{
throw ex;
}
}
}
public class FileUploadCommand
{
public static FileUploadCommand CreateFromFile(string file)
{
var f = new FileUploadCommand()
{
Bytes = File.ReadAllBytes(file),
Extension = Path.GetExtension(file)
};
return f;
}
public byte[] Bytes { get; set; }
public string Extension { get; set; }
}
}
using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
namespace UploadImageTest
{
internal class Program
{
private static void Main(string[] args)
{
BlobHelper.UploadPhoto(
FileUploadCommand.CreateFromFile(@"C:\Users\Angus\Desktop\t.png")
//,FileUploadCommand.CreateFromFile(@"C:\Users\Angus\Desktop\appprodutor.png")
)
.Wait();
}
}
}
@angusbreno
Copy link
Author

Instalar
Microsoft.AspNet.WebApi.Client

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment