C# ASP.NETCore-将上载的文件作为多部分/表单数据发送到API端点
我需要从ASP.NET核心MVC向API端点发送请求,该端点需要多部分/表单数据,格式如下C# ASP.NETCore-将上载的文件作为多部分/表单数据发送到API端点,c#,.net,api,C#,.net,Api,我需要从ASP.NET核心MVC向API端点发送请求,该端点需要多部分/表单数据,格式如下 {boundary value} Content-Disposition: form-data; name='file'; filename='filename.jpg' Content-Type: image/jpeg {file content} --{boundary value}-- 在controller中,我已将文件作为IFormFile接口上传,该接口具有以下属性: Content-T
{boundary value}
Content-Disposition: form-data; name='file'; filename='filename.jpg'
Content-Type: image/jpeg
{file content}
--{boundary value}--
在controller中,我已将文件作为IFormFile接口上传,该接口具有以下属性:
Content-Type
ContentDisposition
如何构造多部分/表单数据并发送
我尝试使用MultipartFormDataContent,它用字符串边界重载了构造函数,但没有成功。公共异步任务PostMultipart(字符串apiendpoint,字节[]数据)
public async Task<HttpResponseMessage> PostMultipart(string apiendpoint, byte[] data)
{
var multipartContent = new MultipartFormDataContent(); // your boundary value if need anything can be passed in the contructore
var fileContent = new ByteArrayContent(data);
fileContent.Headers.ContentType =
MediaTypeHeaderValue.Parse("image/jpeg");
multipartContent.Add(fileContent, "file", "filename.jpg");
//client is HttpClient static field in the class
return await client.PostAsync(apiendpoint, multipartContent);
}
{
var multipartContent=new MultipartFormDataContent();//如果需要,可以在构造函数中传递边界值
var fileContent=newbytearraycontent(数据);
fileContent.Headers.ContentType=
MediaTypeHeaderValue.Parse(“图像/jpeg”);
添加(fileContent,“file”,“filename.jpg”);
//client是类中的HttpClient静态字段
return wait client.PostAsync(apident,multipartContent);
}