c#

C# webclient能上传文件吗

小樊
121
2024-07-12 20:52:27
栏目: 编程语言

是的,C#中的WebClient类可以用来上传文件。可以使用UploadFile或UploadData方法来上传文件到服务器。以下是一个示例代码:

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.");
    }
}

在上面的示例中,我们创建了一个WebClient对象,并使用UploadFile方法将文件上传到指定的URL。您可以根据需要修改文件路径和上传URL。

0
看了该问题的人还看了