I have a webpage that allows drop down elements to dynamically create HTML. On this page I also have a "create File" button. When I click this button, I want it to make an AJAX POST request and upload this dynamic page I created with drag and drops to be uploaded as a File.
Here is a simple HTML file to visualize how the file will look like:
<!doctype html>
<html>
<head>
<title>Test Drag and Drop</title>
</head>
<body>
<div class="dynamicPage">
<div class="subSections">
</div>
<div class="subSections">
</div>
<!-- more elements and objects in here -->
</div>
<div class="stripFromPage">
<!-- More buttons for drag and drop elements to main page -->
<button onclick="createFile(this)">Create File</button>
</div>
</body>
<script>
function createFile(elem){
let url = "action/saveFile";
var formData = new FormData();
formData.append('filename', 'test.html');
formData.append('file', $("html").html());
$.ajax({
type: 'POST',
url: url,
data: formData,
contentType: false,
processData: false,
success: function (data) {
alert(data);
}
});
}
</script>
On the server side I have node.js with a upload method that works for IOS app using the form method, so I know that part is good.
The part I need help with is how to upload DOM object as file and append it to form correctly, specify the name of this file so the backend can save it as that filename.
I prefer javascript over jquery. But I couldn't find any good javascript code for AJAX file upload, so I have jquery here.