I am attempting to upload an Excel file using XMLHttpRequest and FormData in ASP.NET, but I keep encountering the issue of Request.Files being null in ASP.NET. Below is the code I am using - can someone please assist me with this?
function BulkUploadUsers(e){
var url = "/BC/Product/Modules/UserManagement/BulkUpload.aspx?action=import";
var fd = new FormData();
fd.append("ImportWorkOrderExcelFile", document.getElementById('ctl01_ImportFcpFile').files[0]);
var xhr = new XMLHttpRequest();
xhr.open("POST", url, true);
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhr.setRequestHeader('X-CSRF-Token', document.getElementById("_RequestVerificationToken").value);
xhr.setRequestHeader("Content-Type", "multipart/form-data");
xhr.send(fd);
}
<div>
<input type="file" id="ImportFcpFile" runat="server" onchange="BulkUploadUsers(this)"/>
</div>
On the server side in the ASP.NET page:
protected void Page_Load(object sender, EventArgs e)
{
try
{
var aa = FindControl("ImportFcpFile");
HttpPostedFile file = Request.Files["ImportUserExcelFile"];
}
catch (Exception ex)
{
}
}