Check the jquery code below. Here, I am grabbing file from html and then post it by ajax call to my Controller Post method. And from Controller post method i am successfully receiving that file in variable called files as you can see. But my question is how can i send another two text parameters called- type and id from this ajax call then also how can i get that value from controller with that file? Basically i have to grab that file with those text value also. How is that possible? What change need in ajax and controller?
Html:
<div class="col-sm-3" style="float:left;">
<label class="btn btn-outline-dark btn-block">
Browse...
<span id="uploaded-file-name" style="font-style: italic"></span>
<input id="file-upload" type="file" name="file"
onchange="$('#uploaded-file-name').text($('#file-upload')[0].value);" hidden>
</label>
</div>
<div class="col-sm-2" style="float:left;">
<button class="btn btn-dark" id="start_upload">Start Upload</button>
</div>
Jquery ajax:
//file upload
$("#start_upload").click(function (evt) {
var fileUpload = $("#file-upload").get(0);
var files = fileUpload.files;
var data = new FormData();
for (var i = 0; i < files.length; i++) {
data.append(files[i].name, files[i]);
}
$.ajax({
type: "POST",
url: "/Products/UploadFiles",
contentType: false,
processData: false,
data: data,
success: function (message) {
alert(message);
},
error: function () {
alert("There was error uploading files!");
}
});
});
MVC .net core controller:
[HttpPost]
public IActionResult UploadFiles()
{
//file upload process
var files = Request.Form.Files;
string type = "";
int id = ;
}