0% found this document useful (0 votes)
83 views

C# Function To Upload A File To

This C# function uploads a file to anonfile.com and returns a download link. It uses a WebClient to upload the file to the anonfile API URL. The response is parsed to extract the download link if successful, or an error message if unsuccessful. Over multiple edits, the API URL was updated and typos in the error message were corrected.

Uploaded by

hsuyip
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views

C# Function To Upload A File To

This C# function uploads a file to anonfile.com and returns a download link. It uses a WebClient to upload the file to the anonfile API URL. The response is parsed to extract the download link if successful, or an error message if unsuccessful. Over multiple edits, the API URL was updated and typos in the error message were corrected.

Uploaded by

hsuyip
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

C# Function to upload a file to anonfile.

com

static string CreateDownloadLink(string File) // Important to note AnonFile has


a 20gb file size limit (which is overkill for most applications)
{
string ReturnValue = string.Empty;
try
{
using (WebClient Client = new WebClient())
{
byte[] Response =
Client.UploadFile("https://api.anonfiles.com/upload", File);// Thanks noobencoder
for pointing out that Anonfile changed domains since when I originally wrote this 3
years ago. It's fixed now.
string ResponseBody = Encoding.ASCII.GetString(Response);
if (ResponseBody.Contains("\"error\": {"))
{
ReturnValue += "There was a erorr while uploading the
file.\r\n";
ReturnValue += "Error message: " + ResponseBody.Split('"')
[7] + "\r\n";
}
else
{
ReturnValue += "Download link: " + ResponseBody.Split('"')
[15] + "\r\n";
ReturnValue += "File name: " + ResponseBody.Split('"')[25]
+ "\r\n";
}
}
}
catch (Exception Exception)
{
ReturnValue += "Exception Message:\r\n" + Exception.Message + "\r\
n";
}
return ReturnValue;
}

============First Edit =============

static string CreateDownloadLink(string File)


{
string ReturnValue = string.Empty;
try
{
using (WebClient Client = new WebClient())
{
byte[] Response =
Client.UploadFile("https://api.anonfiles.com/upload", File); //Edited (Working*)
string ResponseBody = Encoding.ASCII.GetString(Response);
if (ResponseBody.Contains("\"error\": {"))
{
ReturnValue += "There was a erorr while uploading the
file.\r\n";
ReturnValue += "Error message: " + ResponseBody.Split('"')
[7] + "\r\n";
}
else
{
ReturnValue += "Download link: " + ResponseBody.Split('"')
[15] + "\r\n";
ReturnValue += "File name: " + ResponseBody.Split('"')[25]
+ "\r\n";
}
}
}
catch (Exception Exception)
{
ReturnValue += "Exception Message:\r\n" + Exception.Message + "\r\
n";
}
return ReturnValue;
}

==================================Second edit =======

static string CreateDownloadLink(string File)


{
string ReturnValue = string.Empty;
try
{
using (WebClient Client = new WebClient())
{
byte[] Response =
Client.UploadFile("https://api.anonfiles.com/upload", File); //Edited (Working*)
string ResponseBody = Encoding.ASCII.GetString(Response);
if (ResponseBody.Contains("\"error\": {"))
{
ReturnValue += "There was a erorr while uploading the
file.\r\n";
ReturnValue += "Error message: " + ResponseBody.Split('"')
[7] + "\r\n";
}
else
{
ReturnValue += "Download link: " + ResponseBody.Split('"')
[15] + "\r\n";
ReturnValue += "File name: " + ResponseBody.Split('"')[25]
+ "\r\n";
}
}
}
catch (Exception Exception)
{
ReturnValue += "Exception Message:\r\n" + Exception.Message + "\r\
n";
}
return ReturnValue;
}

You might also like