I have developed a small asp.net web forms application for managing emails. The interface allows users to input mandatory information such as sender, recipient, and subject. I am now looking to implement file attachments in the emails using the asp.net file upload controller to upload multiple files.
https://i.sstatic.net/MGCzC.png
To send this data to the server-side code behind without refreshing the page, I have decided to utilize AJAX calls. However, I am facing challenges with sending the attached files along with the form data. After researching, I discovered that I need to use FormData
to properly handle file uploads. I have created a FormData
object and appended the attached files to it, but I am unsure of how to pass this object to the server-side code.
function sendEmail() {
var data = new FormData();
var files = $('.attachment');
$.each(files, function (key, value) {
var file = $(value).data('file');
data.append(file.name, file);
});
$.ajax({
url: "OpenJobs.aspx/sendEmail",
type: "POST",
async: false,
contentType: false,
processData: false,
data: null,
success: function (result) {
alert(result);
},
error: function (err) {
alert(err.statusText);
}
});
}
Any suggestions on how to make this work seamlessly?