0

I am sending a file input in FormData to a web api as below. The api method expects one additional parameter as well but the below code works only without the parameter in api. How can I send the additional parameter to the api. Thanks for any suggestions!

<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>");
        }
});

Web API:

[HttpPost]
public async void Add(string customertype)
{
    var provider = new MultipartFormDataStreamProvider(HttpContext.Current.Server.MapPath("~/App_Data"));

    await Request.Content.ReadAsMultipartAsync(provider);
}

1 Answer 1

1

Found out that below does not work.

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

It has to be specified in url

url: _uri + '/party/Add?customertype=' + 'testtype'

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.