Hello, you need the ability to upload files to the server with a standard uploader to the selected folder, while the upload button should be in the context menu of the folder, like this:
I tried to create an application that moves the downloaded file from the open directory to the selected folder, and then deletes the UI element of the downloaded file. But this method works extremely unstable:
OCA.Files.fileActions.registerAction({
name: 'upload',
displayName: t('files', 'Outload'),
mime: 'dir',
permissions: OC.PERMISSION_UPDATE,
icon: OC.imagePath('core', 'actions/upload'),
actionHandler: async function (fileName, context) {
const targetDir = OC.joinPaths(context.dir, fileName);
try {
const data = await new Promise((resolve, reject) => {
$('#file_upload_start').on('fileuploaddone', function (e, data) {
resolve(data);
});
$('#file_upload_start').on('fileuploadfail', function (e, data) {
reject(data);
});
// Start the file upload process
$('#file_upload_start').trigger('click');
});
const uploadedFileName = data.files[0].name;
const currentDir = OCA.Files.App.fileList.getCurrentDirectory();
const sourcePath = OC.joinPaths(currentDir, uploadedFileName).split('/').map(encodeURIComponent).join('/');
const targetPath = OC.joinPaths(targetDir, uploadedFileName).split('/').map(encodeURIComponent).join('/');
const headers = { 'Destination': OC.linkToRemoteBase('dav/files/' + OC.getCurrentUser().uid + '/' + targetPath) };
await new Promise((resolve, reject) => {
$.ajax({
url: OC.linkToRemoteBase('dav/files/' + OC.getCurrentUser().uid + '/' + sourcePath),
type: 'MOVE',
headers: headers,
}).done(function() {
console.log("File successfully moved");
// Save folder name to local storage
localStorage.setItem('movedToFolder', fileName);
resolve();
}).fail(function(jqXHR, textStatus, errorThrown) {
console.error("Failed to move file: " + textStatus + ", " + errorThrown);
reject(new Error("Failed to move file: " + textStatus + ", " + errorThrown));
});
});
// Hide the file from the interface
const movedToFolder = localStorage.getItem('movedToFolder');
if (movedToFolder) {
const $fileRow = $('tr[data-file="' + movedToFolder + '"]');
$fileRow.css('height', '0');
$fileRow.css('padding', '0');
$fileRow.css('border', '0');
$fileRow.css('opacity', '0');
// Clear the folder name from local storage
localStorage.removeItem('movedToFolder');
}
} catch (error) {
console.error("An error occurred: ", error);
}
}
});