Which is the best practice for sending a file to a custom node form?
In my custom node I would like to allow to send a file to a custom end point (made via RED.admin.post) who parse the file and output a json to the caller.
Is there a way to do this?
EDIT: I solved in this way:
In template .html file
<script type="text/x-red" data-template-name="...">
<form id="myForm">
<input id="myFile" type="file" name="fileToUpload">
<input id="file-submit" type="submit" value="Import data from file" name="submit">
</form>
...
oneditprepare: function () {
$('#file-submit').click(function (e) {
var fd = new FormData();
fd.append('file', $('#myFile')[0].files[0]);
var file = fd.get('file');
var blb = file.slice();
var reader = new FileReader();
// This fires after the blob has been read/loaded.
reader.addEventListener('loadend', (e) => {
var fileData = e.srcElement.result;
$.ajax({
url: '/file-upload',
data: { fileData: fileData},
method: 'POST',
success: function (data) {
populateForm(data);
}
});
});
// Start reading the blob as text.
reader.readAsBinaryString(blb);
});