C# webclient能上傳文件嗎

c#
小樊
121
2024-07-12 20:52:27

是的,C#中的WebClient類可以用來(lái)上傳文件??梢允褂肬ploadFile或UploadData方法來(lái)上傳文件到服務(wù)器。以下是一個(gè)示例代碼:

using System;
using System.Net;

class Program
{
    static void Main()
    {
        WebClient client = new WebClient();
        string filePath = "C:\\path\\to\\file.txt";
        string uploadUrl = "http://example.com/upload";

        client.UploadFile(uploadUrl, filePath);
        Console.WriteLine("File uploaded successfully.");
    }
}

在上面的示例中,我們創(chuàng)建了一個(gè)WebClient對(duì)象,并使用UploadFile方法將文件上傳到指定的URL。您可以根據(jù)需要修改文件路徑和上傳URL。

0