Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
621 views
in Technique[技术] by (71.8m points)

c# - Image Upload using HttpClient MultipartFormDataContent in .Net Core

I am trying to upload an image to MediumApi. But I can't handle it. Here is the medium example request.

POST /v1/images HTTP/1.1
Host: api.medium.com
Authorization: Bearer 181d415f34379af07b2c11d144dfbe35d
Content-Type: multipart/form-data; boundary=FormBoundaryXYZ
Accept: application/json
Accept-Charset: utf-8

--FormBoundaryXYZ
Content-Disposition: form-data; name="image"; filename="filename.png"
Content-Type: image/png

IMAGE_DATA
--FormBoundaryXYZ--
HttpClient client = new HttpClient();

client.DefaultRequestHeaders.Add("Accept", "application/json; charset=utf-8");
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {_token}");
client.DefaultRequestHeaders.Add("ContentType", "multipart/form-data; boundary=FormBoundaryXYZ");

var requestContent = new MultipartFormDataContent("FormBoundaryXYZ");

var imageContent = new ByteArrayContent(File.ReadAllBytes(Startup.ImageFilePath + fileName));
                //streamContent.Headers.ContentDisposition = ContentDispositionHeaderValue.Parse($"form-data; name=image; filename={fileName}");
imageContent.Headers.ContentType = MediaTypeHeaderValue.Parse("image/jpeg");
requestContent.Add(imageContent, "image", fileName);

var response = await client.PostAsync(_imageUrl, requestContent);
string resultCode = response.StatusCode.ToString();
string responseBodyAsText = await response.Content.ReadAsStringAsync();

and this is my code. This code returns Internal Server Error but I didn't understand.

question from:https://stackoverflow.com/questions/65651317/image-upload-using-httpclient-multipartformdatacontent-in-net-core

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...