httpclient上传图片(multipart/form-data)

发布时间 2023-10-09 16:20:56作者: Ace001
string boundary = string.Format("----WebKitFormBoundary{0}", DateTime.Now.Ticks.ToString("x"));
                MultipartFormDataContent content = new MultipartFormDataContent(boundary);
                content.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data; boundary=" + boundary);
                string fileName = Path.GetFileName(picPath);

                using FileStream fStream = File.Open(picPath, FileMode.Open, FileAccess.Read);
                content.Add(new StreamContent(fStream, (int)fStream.Length), "file", fileName);

                using HttpClient client = new HttpClient();
                HttpResponseMessage response = client.PostAsync(url, content).Result;

                if (response.IsSuccessStatusCode)
....