called webapi sending input type files="file"

1

Good afternoon,

I have the following block of code:

 @using (Html.BeginForm("Upload", "Orders", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
      <input type="file" name="attach" id="attach" multiple />
      <input id="btnAttach" value="Upload" />
    }

and I want that instead of using the html.beginform to use a $ ajax to consume it, my question is how I create the data based on the files that I select in my input id = attach to consume my web service

$("$btnAttach").on("click", function() {
        $.ajax({
            url: "/order/Upload/",
            type: "POST",
            data: /* form data here */,
            success: SuccessCallback,
            error: FailureCallback
        });
    });

since with the first method it gives me a lot of trouble to return an approved response to the html to be processed

    
asked by Angelo cardona 20.09.2016 в 17:12
source

1 answer

0

I would recommend you take a look

[ASP.NET Web API] Uploading file with jQuery and Web API

you will notice that you use

    var files = $("#inputFile").get(0).files;
    var data = new FormData();
    for (i = 0; i < files.length; i++) {
        data.append("file" + i, files[i]);
    }

to put together the data that you send in the $.ajax , to achieve it use the object FormData

Using FormData Objects

    
answered by 23.09.2016 в 13:22