Web API 如何使用 Content-Type: multipart/form-data 请求正文参数访问?
How request body parameter access by Web API using Content-Type: multipart/form-data?
当我以 JSON 格式从请求正文传递参数时,没有访问 Web api。我的代码:
public async Task<HttpResponseMessage> Upload([FromBody]AddPhotoBizPrm addPhotoBizPrm)
{
json_obj.RESPONSE = new { };
string images = "";
if (Request.Content.IsMimeMultipartContent())
{
var provider = new MultipartMemoryStreamProvider();
await Request.Content.ReadAsMultipartAsync(provider);
foreach (var file in provider.Contents)
{
var filename = file.Headers.ContentDisposition.FileName.Trim('\"');
var buffer = file.ReadAsByteArrayAsync();
images += filename;
//Do whatever you want with filename and its binary data.
}
}
json_obj.RESPONSE = images;
json_obj.MESSAGE = "";
json_obj.STATUS_CODE = 701;
return new HttpResponseMessage()
{
Content = new StringContent("ok")
};
}
我正在从请求正文传递参数:
{
deviceid:"",
rate:2,
review:"",
skuid:123,
userId:875904
}
解决了这个问题。
我使用的是 content-type=multipart/form-data 所以我们不能将 json 参数传递给请求正文。
所以我们将以数据形式传递参数
deviceid=""&rate=2&review=""&skuid=123&userId=875904
当我以 JSON 格式从请求正文传递参数时,没有访问 Web api。我的代码:
public async Task<HttpResponseMessage> Upload([FromBody]AddPhotoBizPrm addPhotoBizPrm)
{
json_obj.RESPONSE = new { };
string images = "";
if (Request.Content.IsMimeMultipartContent())
{
var provider = new MultipartMemoryStreamProvider();
await Request.Content.ReadAsMultipartAsync(provider);
foreach (var file in provider.Contents)
{
var filename = file.Headers.ContentDisposition.FileName.Trim('\"');
var buffer = file.ReadAsByteArrayAsync();
images += filename;
//Do whatever you want with filename and its binary data.
}
}
json_obj.RESPONSE = images;
json_obj.MESSAGE = "";
json_obj.STATUS_CODE = 701;
return new HttpResponseMessage()
{
Content = new StringContent("ok")
};
}
我正在从请求正文传递参数:
{
deviceid:"",
rate:2,
review:"",
skuid:123,
userId:875904
}
解决了这个问题。 我使用的是 content-type=multipart/form-data 所以我们不能将 json 参数传递给请求正文。 所以我们将以数据形式传递参数 deviceid=""&rate=2&review=""&skuid=123&userId=875904