Home > OS >  C# How to send a file to webhook and get sent file's link?
C# How to send a file to webhook and get sent file's link?

Time:04-03

Hello im trying upload file to a link and i tried this:

`private void buttonInput_Click(object sender, EventArgs e)
    {
        try
        {
            using (WebClient client = new WebClient())
            {
                var resStr = client.UploadFile(@"https://anonfiles.com", @"C:\Users\sadettin\desktop\test.txt");
                var jObjResult = JObject.Parse(Encoding.UTF8.GetString(resStr));
                var linkToFile = jObjResult["link"];
            }
        }
        catch(Exception err)
        {
            MessageBox.Show(err.Message);
        }
    }`

But Im taking 404 error.

Now i want to send any txt file to my discord webhook address and take sent file's link.

How can i do?

CodePudding user response:

Despite your claims, using the correct end-point and a non-zero bytes file does lead to an uploaded file:

using (WebClient client = new WebClient())
{
      var resStr = client.UploadFile(@"https://api.anonfiles.com/upload", @"C:\tmp\test.txt");
      var jObjResult = JObject.Parse(Encoding.UTF8.GetString(resStr));
      var linkToFile = jObjResult["data"]["file"]["url"]["full"].ToString();
      MessageBox.Show(linkToFile);
}

Do note that the JSON structure that is returned is different then you seem to handle. The url is found in an attribute full under this path /data/file/url hence this line in my code example:

var linkToFile = jObjResult["data"]["file"]["url"]["full"];

Here is one of the full urls that the service returned to me with my test file

https://anonfiles.com/nai0Z3S0x5/test_txt

It is 106 bytes in total.

  • Related