After adding a debugger in the console, I am receiving confirmation that the file has been uploaded successfully. However, the debugger is not reaching the code behind, or in other words, the code behind is not accessible.
This is the JavaScript file:
function uploadFile() {
debugger
var fileInput = document.getElementById("myFile");
var file = fileInput.files[0];
if (file) {
var applicationID = selectedApplicationID;
var orderID = $("#txtOrderID").val();
var companyID = $("#tdCompanyID").text();
var orderedDate = $("#tdOrderedDate").text();
var instaProductName = $("#tdReport").text();
var data = new FormData();
data.append("ApplicationID", applicationID);
data.append("CompanyID", companyID);
data.append("OrderID", orderID);
data.append("OrderedDate", orderedDate);
data.append("ProductName", instaProductName);
data.append("uploadedFile", file);
// Make an AJAX request to the server to upload the file
$.ajax({
type: "POST",
url: "InstaReportsDownloader.aspx/UploadFile",
data: data,
contentType: false,
processData: false,
success: function (response) {
// Handle the success response
console.log("File uploaded successfully!");
console.log(response);
},
error: function (xhr, status, error) {
// Handle the error response
console.log("File upload failed!");
console.log(error, status, xhr);
}
});
}
}
And this is the code behind:
[System.Web.Services.WebMethod(EnableSession = true)]
public string UploadFile(int ApplicationID, int CompanyID, int OrderID, DateTime OrderedDate, string ProductName, HttpPostedFile uploadedFile)
{
string DataDirectory = ConfigurationManager.AppSettings["DataDirectory"].ToString();
string folderPath = null;
if (ApplicationID == 4)
{
folderPath = Path.Combine(DataDirectory, "XML Reports", CompanyID.ToString(), ProductName);
}
else if (ApplicationID == 5)
{
folderPath = Path.Combine(DataDirectory, "InstaAPI Reports", CompanyID.ToString(), ProductName);
}
// Create the directory if it doesn't exist
Directory.CreateDirectory(folderPath);
// Generate the file name based on the OrderedDate
string fileName = $"Report_{OrderedDate.ToString("mm/dd/yyyy")}.xml";
string filePath = Path.Combine(folderPath, fileName);
// Save the uploaded file to the specified path, overwriting if it already exists
uploadedFile.SaveAs(filePath);
return filePath;
}