async Task<string> GetAndSaveImageAsync(string url)
{
string imageName = $"{Guid.NewGuid()}.png";
string filePath = System.IO.Path.Combine(_webHostEnvironment.ContentRootPath, "Resources","Images");
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
var httpRequest = new HttpRequestMessage(HttpMethod.Get, url);
using (var httpClient = new HttpClient())
{
var response = httpClient.SendAsync(httpRequest).Result;
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
var imageBytes = await response.Content.ReadAsByteArrayAsync();
filePath = System.IO.Path.Combine(filePath, imageName);
File.WriteAllBytes(filePath, imageBytes);
}
}
return imageName;
}
Comments
Post a Comment