C# web api post 图像作为表单数据
C# web api post image as form data
在 postman 中很容易做到:
为了通过网络实现它api,我写了下面的代码:
HttpClient client = new HttpClient();
string url = "https://upload.*******.com/api/upload/photos";
client.BaseAddress = new Uri(url);
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("multipart/form-data"));
byte[] data;
using (var br = new BinaryReader(inputPhotoUpload.files.OpenReadStream()))
data = br.ReadBytes((int) inputPhotoUpload.files.OpenReadStream().Length);
ByteArrayContent bytes = new ByteArrayContent(data);
MultipartFormDataContent multipartFormDataContent = new
MultipartFormDataContent();
multipartFormDataContent.Add(new StringContent(inputPhotoUpload.watermark.ToString()), "watermark");
multipartFormDataContent.Add(new StringContent("listing"), "type");
multipartFormDataContent.Add(bytes, "files", inputPhotoUpload.files.FileName);
HttpResponseMessage res = await client.PostAsync(url, multipartFormDataContent);
我的 InputPhotoUpload class
public class InputPhotoUpload
{
public string watermark { get; set; }
public string type { get; set; }
public IFormFile files { get; set; }
}
上传服务器接受字节数组中的图像和另外两个名为 listing
和 watermark
的值。服务器给出代码 500 内部错误。
我的目标是 post 表单数据 post 上传图片,post 人可以做到但我的控制器 api 不能。
显然我需要将所有 headers 与邮递员同步,有些是不必要的。
HttpClient client = new HttpClient();
MultipartFormDataContent form = new MultipartFormDataContent();
client.DefaultRequestHeaders.Clear();
ByteArrayContent bytes = new ByteArrayContent(imageStream);
form.Add(bytes, "files", imageName);
form.Add(new StringContent("En"), "watermark");
form.Add(new StringContent("listing"), "type");
HttpResponseMessage response = await client.PostAsync("https://upload.*****.com/api/upload/photos", form);
var k = response.Content.ReadAsStringAsync().Result;
_Result = JsonConvert.DeserializeObject<List<Output>>(k);
在 postman 中很容易做到:
为了通过网络实现它api,我写了下面的代码:
HttpClient client = new HttpClient();
string url = "https://upload.*******.com/api/upload/photos";
client.BaseAddress = new Uri(url);
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("multipart/form-data"));
byte[] data;
using (var br = new BinaryReader(inputPhotoUpload.files.OpenReadStream()))
data = br.ReadBytes((int) inputPhotoUpload.files.OpenReadStream().Length);
ByteArrayContent bytes = new ByteArrayContent(data);
MultipartFormDataContent multipartFormDataContent = new
MultipartFormDataContent();
multipartFormDataContent.Add(new StringContent(inputPhotoUpload.watermark.ToString()), "watermark");
multipartFormDataContent.Add(new StringContent("listing"), "type");
multipartFormDataContent.Add(bytes, "files", inputPhotoUpload.files.FileName);
HttpResponseMessage res = await client.PostAsync(url, multipartFormDataContent);
我的 InputPhotoUpload class
public class InputPhotoUpload
{
public string watermark { get; set; }
public string type { get; set; }
public IFormFile files { get; set; }
}
上传服务器接受字节数组中的图像和另外两个名为 listing
和 watermark
的值。服务器给出代码 500 内部错误。
我的目标是 post 表单数据 post 上传图片,post 人可以做到但我的控制器 api 不能。
显然我需要将所有 headers 与邮递员同步,有些是不必要的。
HttpClient client = new HttpClient();
MultipartFormDataContent form = new MultipartFormDataContent();
client.DefaultRequestHeaders.Clear();
ByteArrayContent bytes = new ByteArrayContent(imageStream);
form.Add(bytes, "files", imageName);
form.Add(new StringContent("En"), "watermark");
form.Add(new StringContent("listing"), "type");
HttpResponseMessage response = await client.PostAsync("https://upload.*****.com/api/upload/photos", form);
var k = response.Content.ReadAsStringAsync().Result;
_Result = JsonConvert.DeserializeObject<List<Output>>(k);