public UploadFile ( System address, string fileName ) : byte[] | ||
address | System | |
fileName | string | |
return | byte[] |
using System; using System.Net; public class Program { static void Main(string[] args) { WebClient client = new WebClient(); string url = "http://example.com/upload"; string file = @"C:\Users\JohnDoe\Documents\example.txt"; string username = "John"; string password = "Doe"; client.Credentials = new NetworkCredential(username, password); byte[] response = client.UploadFile(url, file); Console.WriteLine("Response: " + System.Text.Encoding.ASCII.GetString(response)); } }
using System; using System.Net; using System.Threading.Tasks; public class Program { static void Main(string[] args) { Task.Run(() => { WebClient client = new WebClient(); string url = "http://example.com/upload"; string file = @"C:\Users\JohnDoe\Documents\example.txt"; byte[] response = client.UploadFile(url, file); Console.WriteLine("Response: " + System.Text.Encoding.ASCII.GetString(response)); }); Console.WriteLine("File upload started."); Console.ReadLine(); } }This example demonstrates how to upload a file asynchronously using the `Task.Run` method. The code starts the file upload in a new task, allowing the main thread to continue executing. The response from the server is written to the console. The package library used in these examples is the .NET Framework Class Library. The `System.Net` namespace provides types for managing network connections, and the `WebClient` class is included in the library for making HTTP requests.
public UploadFile ( System address, string fileName ) : byte[] | ||
address | System | |
fileName | string | |
return | byte[] |