【发布时间】:2020-10-10 00:42:39
【问题描述】:
我正在将 FormData 中的文件输入发送到 Web api,如下所示。 api 方法也需要一个额外的参数,但下面的代码仅在没有 api 参数的情况下有效。如何将附加参数发送到 api。 感谢您的任何建议!
<div>
<label for="add">Add Customer</label>
<input type="file" onchange="AddCust(event)" />
</div>
function AddCust(event)
{
Add("testtype", event.target.files[0]);
}
function Add(type, file)
{
var imageData = new FormData();
imageData.append("myfile", file);
$.ajax({
url: _uri + '/party/Add',
type: 'POST',
enctype: 'multipart/form-data',
data: imageData,
cache: false,
contentType: false,
processData: false,
crossDomain: true,
xhrFields: { withCredentials: true },
success: function (data) {
$("#log").append("Add - Success " + data.toString() + "</br>");
},
error: function (xhr, ajaxOptions, thrownError) {
$("#log").append("Add - Error " + xhr.responseText + "</br>");
}
});
网络 API:
[HttpPost]
public async void Add(string customertype)
{
var provider = new MultipartFormDataStreamProvider(HttpContext.Current.Server.MapPath("~/App_Data"));
await Request.Content.ReadAsMultipartAsync(provider);
}
【问题讨论】:
标签: ajax asp.net-web-api multipartform-data