You could use form submit or use ajax to post the form and use IFormFile
on action to receive your file.In the action, then you could get file name or convert file to byte[]
.
The name of action parameter is required to match the name of form data.
For example:
Js:
<script>
$('#submitAddFile').click(function (e) {
e.preventDefault();
var file = $('#fileInput')[0].files;
var formData = new FormData();
formData.append("myFile", file[0]);
$.ajax({
type: "POST",
url: "/Home/UploadFile",
contentType: false,
processData: false,
data: formData,
success: function (result) {
alert("success");
},
error: function () {
alert("there was an error");
}
});
})
</script>
HomeController:
[HttpPost]
public void UploadFile(IFormFile myFile)
{
var fileName = Path.GetFileName(myFile.FileName);
using (var ms = new MemoryStream())
{
myFile.CopyTo(ms);
byte[] fileBytes = ms.ToArray();
//save fileName and fileBytes into database
}
}