Last active
January 19, 2018 12:55
-
-
Save angusbreno/aeb7f8adfda38db46ae0fc010891687f to your computer and use it in GitHub Desktop.
UploadFile WebAPI - Client
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
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; } | |
} | |
} |
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
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(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Instalar
Microsoft.AspNet.WebApi.Client