以下是使用 C# 和 HttpClient 上传多个图片的示例代码:

using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;

namespace UploadMultipleImages
{
    class Program
    {
        static async Task Main(string[] args)
        {
            var images = new List<string> { 'image1.jpg', 'image2.png', 'image3.gif' };
            var apiUrl = 'https://example.com/upload';

            using (var httpClient = new HttpClient())
            {
                using (var formData = new MultipartFormDataContent())
                {
                    foreach (var image in images)
                    {
                        var fileContent = new ByteArrayContent(File.ReadAllBytes(image));
                        fileContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue('image/jpeg'); // replace with actual image type
                        formData.Add(fileContent, 'images', Path.GetFileName(image));
                    }

                    var response = await httpClient.PostAsync(apiUrl, formData);
                    if (response.IsSuccessStatusCode)
                    {
                        Console.WriteLine('Upload successful!');
                    }
                    else
                    {
                        Console.WriteLine('Upload failed!');
                    }
                }
            }

            Console.ReadLine();
        }
    }
}

此代码将创建一个 HttpClient 实例,并使用 MultipartFormDataContent 创建一个包含多个文件的表单数据。然后,它将使用 PostAsync 方法将表单数据上传到指定的 API 端点。请注意,此示例假定所有图像都存储在与应用程序相同的目录中。您需要根据自己的需求更改文件路径和 API 端点。

C# 使用 HttpClient 上传多个图片 - 代码示例

原文地址: https://www.cveoy.top/t/topic/oZzE 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录