Files
crysome/Crysome.Client/Crysome.Client.Web/WebFileDownloader.cs
T
2026-08-27 11:22:54 -06:00

55 lines
1.2 KiB
C#

using System;
using System.IO;
using System.Net.Http;
namespace Crysome.Client.Web;
public class WebFileDownloader
{
private readonly HttpClient httpClient;
public WebFileDownloader()
{
httpClient = new HttpClient();
}
public async void DownloadFile(string url)
{
HttpResponseMessage response = await httpClient.GetAsync(url);
if (!response.IsSuccessStatusCode)
{
return;
}
byte[] bytes = await response.Content.ReadAsByteArrayAsync();
string[] array = url.Split('/');
string text = array[array.Length - 1];
_ = string.Empty;
_ = string.Empty;
string text2;
if (text.Contains("."))
{
string[] array2 = text.Split('.');
text2 = array2[0];
if (array2[1].Contains("?"))
{
array2[1] = array2[1].Split('?')[0];
}
string text3 = array2[1];
text2 = text2 + "." + text3;
}
else
{
text2 = text;
}
if (response.Content.Headers.ContentDisposition != null)
{
text2 = response.Content.Headers.ContentDisposition.FileName;
}
if (response.Content.Headers.ContentType != null)
{
MimeTypeMap.GetExtension(response.Content.Headers.ContentType.MediaType);
}
File.WriteAllBytes(Path.Combine(Environment.CurrentDirectory, text2), bytes);
}
}