【问题标题】:How can we send FormData to a web api with additional parameters?我们如何将 FormData 发送到带有附加参数的 web api?
【发布时间】: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


    【解决方案1】:

    发现下面的不行。

    url: _uri + '/party/Add/testtype'

    必须在url中指定

    网址:_uri + '/party/Add?customertype=' + 'testtype'

    【讨论】:

      猜你喜欢
      • 2019-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-12
      • 1970-01-01
      • 2021-01-03
      相关资源
      最近更新 更多