Skip to content

Commit

Permalink
Create CatboxUploader.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
Metalloriff authored May 8, 2022
1 parent 78064cc commit 7449df0
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions CatboxUploader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.IO;
using System.Net.Http;

public class CatboxUploader
{
// NOTE - You can also return Task<string> instead of void and remove the callback to just await this method
public static async void UploadToCatbox(string filePath, Action<string> callback)
{
using var client = new HttpClient();
client.BaseAddress = new Uri("https://catbox.moe/user/api.php");

using var content = new MultipartFormDataContent();

content.Add(new StringContent("fileupload"), "reqtype");
content.Add(new StreamContent(File.OpenRead(filePath)), "fileToUpload", Path.GetFileName(filePath));

var response = await client.PostAsync("", content);
if (response.IsSuccessStatusCode)
{
var url = await response.Content.ReadAsStringAsync();
callback(url);
}
else
{
// Handle your failed requests here
}
}
}

0 comments on commit 7449df0

Please sign in to comment.