Please help/suggest to resolve below file upload cases in Frappe…
File Upload cases::
Upload file on specific path/folder/different server drive , we do not want to upload in default folder
We want to upload files to a remote file servers
Create Dynamic folder path (if not exist) and uploads the files in that path, if path already exists the upload the file in that folder path
Rename the file before uploading to remove the clash of file names in same folder
You can create custom scripts for the File doctype. You can examine the Frappe Drive code folder to examine it.
Also there is some integrations with NextCloud, Google Drive, Dropbox. Search for them in the forum and marketplace.
Lastly, if all fails I would integrate a custom solution with n8n.
avc
May 9, 2023, 9:00pm
3
Hi:
I think you should consider something like this:
On your frontend, client script or .js file … . Use FileUploader
new frappe.ui.FileUploader({
method: 'testfile.handler.capture',
make_attachments_public: "False",
dialog_title: "Your title",
disable_file_browser: "False",
frm: frm,
restrictions: {
allowed_file_types: [".pdf"]
},
on_success (file) {
frappe.show_alert({
message:__('File uploaded.'),
indicator:'green'
}, 5);
}
});
This code show dialog to attach the file and pass it to the backend.
On your backend, create a method that manage this file as needed. This way:
@frappe.whitelist()
def capture(file = None):
file_content = frappe.local.uploaded_file
file_name = frappe.local.uploaded_filename
file_path = frappe.local.site + "/" + file_name
# your magic here to rename, check if file exists, change path ...
with open(file_path, "wb") as file:
file.write(file_content)
Hope this helps.
3 Likes
Hi avc,
Could you tell me method: ‘testfile.handler.capture’, means?
testfile : doctype?
handler : ?
capture: ?
and where the def capture(file = None): shoud be defined? the doc type .py?
avc
December 31, 2024, 9:14am
6
Hi @gaoyancd :
testfile → Your app/module
handler → .py file
capture → method inside handler.py
You can use a doctype .py file . Remember that frappe.whitelist()
decorator will be needed to get access from frontend.
Hope this helps.
3 Likes
Thank you! It’s working. @avc