我正在尝试使用
axios
在react native as中
multipart/form-data
到ASP.NET Web API。
我一直在关注这个-
https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/sending-html-form-data-part-2
我正在用
MultipartFormDataStreamProvider
似乎同时得到了文件和表单数据。但是,表单数据不完整。例如,我向服务器发送了大约14个字段,但是当我试图通读时,只得到了7个
provider.FormData
是的。
我的react native/axios代码
var dataToSubmit = new FormData();
// Have about 14 fields in the FormData
dataToSubmit.append('Key1', 'value1');
dataToSubmit.append('Key2', 'value2');
dataToSubmit.append('Key3', 'value3');
.
.
dataToSubmit.append('Key14', 'value14');
// Have 1 file in the FormData
dataToSubmit.append('File', {
uri: filePath,
type: 'image/jpeg',
name: fileName
});
axios({
method: 'POST',
url: 'URL to post',
data: dataToSubmit,
headers: {
'Content-Type': 'multipart/form-data'
}
}).then((response) => {
console.log(response);
});
我的请求
我在电话上有一个调试器,我看到了以下请求(包括所有14个字段和文件)
POST /api/urltopost HTTP/1.1
accept: application/json, text/plain, */*
Content-Type: multipart/form-data;
boundary=BOUNDARY_GUID
Content-Length: 53038
Host: HOST_IP
Connection: Keep-Alive
Accept-Encoding: gzip
--BOUNDARY_GUID
content-disposition: form-data; name="Key1"
Content-Length: 24
--BOUNDARY_GUID
content-disposition: form-data; name="Key2"
Content-Length: 24
..
..
--BOUNDARY_GUID
content-disposition: form-data; name="File"
filename="test.jpg",
Content-Type: image/jpeg
Content-Length: 50626
我的ASP.NET Web API代码
public async Task<HttpResponseMessage> PostFormData(DTO obj)
{
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
string root = HttpContext.Current.Server.MapPath("~/App_Data");
var provider = new MultipartFormDataStreamProvider(root);
try
{
await Request.Content.ReadAsMultipartAsync(provider);
// Show all the key-value pairs.
foreach (var key in provider.FormData.AllKeys)
{
foreach (var val in provider.FormData.GetValues(key))
{
Trace.WriteLine(string.Format("{0}: {1}", key, val));
}
}
return Request.CreateResponse(HttpStatusCode.OK);
}
catch (System.Exception e)
{
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
}
}
当我看着
provider
,显示
Contents
数到8,
FileData
计数1和
FormData
有7个字段。
但我好像少了其他7个字段。
注:
-
我在用奥文
-
我试着把文件从21kb上传到52mb。我总是得到8个内容。我甚至试过
多部分/表单数据
提交没有文件,我仍然只得到那些字段。
-
我在我的
web.config
设置请求限制,这似乎对提供程序的缓冲区大小没有影响-
<!-- Under system.webServer -->
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="2147483648" />
</requestFiltering>
</security>
<!-- Under system.web -->
<httpRuntime targetFramework="4.5.2" maxRequestLength="2097152" />
-
我试着用
MultipartMemoryStreamProvider
它仍然得到8个内容。
-
我试过使用postman,但仍然在chunk/8字段中获取表单数据。
-
我试图将缓冲区大小设置为
5120000
(因为它是
4098
默认情况下)用于
多部分窗体数据流提供程序
我仍然只得到那8块地。我似乎找不到其他表单字段出现的原因/我无法在服务器端获取它们的值。我很确定我在客户方面没有做错任何事,因为它也不适用于邮递员。所以我猜这就是我在api上读取多部分数据的方式。
有人能告诉我我错过了什么/做错了什么吗?为什么我看不到表格上所有的字段?