I am working with ASP.Net Core 2.1, and trying to upload a file while returning it\'s url, without refreshing the page.
I am trying to write the JavaScript in site.js a
Sharing the code that worked for me, implementing @Shyju's answer.
View ( Razor Page ):
AJAX code added in Site.js (to make it a reusable):
// The function takes Form and the event object as parameter
function SubmitForm(frm, caller) {
caller.preventDefault();
var fdata = new FormData();
var file = $(frm).find('input:file[name="uploadfile"]')[0].files[0];
fdata.append("file", file);
$.ajax(
{
type: frm.method,
url: frm.action,
data: fdata,
processData: false,
contentType: false,
success: function (data) {
alert(data);
},
error: function (data) {
alert(data);
}
})
};