Skip to content

Commit 7449df0

Browse files
authored
Create CatboxUploader.cs
1 parent 78064cc commit 7449df0

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

CatboxUploader.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.IO;
3+
using System.Net.Http;
4+
5+
public class CatboxUploader
6+
{
7+
// NOTE - You can also return Task<string> instead of void and remove the callback to just await this method
8+
public static async void UploadToCatbox(string filePath, Action<string> callback)
9+
{
10+
using var client = new HttpClient();
11+
client.BaseAddress = new Uri("https://catbox.moe/user/api.php");
12+
13+
using var content = new MultipartFormDataContent();
14+
15+
content.Add(new StringContent("fileupload"), "reqtype");
16+
content.Add(new StreamContent(File.OpenRead(filePath)), "fileToUpload", Path.GetFileName(filePath));
17+
18+
var response = await client.PostAsync("", content);
19+
if (response.IsSuccessStatusCode)
20+
{
21+
var url = await response.Content.ReadAsStringAsync();
22+
callback(url);
23+
}
24+
else
25+
{
26+
// Handle your failed requests here
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)