class Student(Document):
def custom_file_uploader(self):
student_name = self.student_name
state = self.state
file_url = frappe.doc.get('attach')
file_content = get_file(file_url)
file_name_old = frappe.local.uploaded_filename
file_name = f"{student_name}-{file_name_old}"
# Create the folder based on the state field value
folder_path = f"/home/frappe3/frappe-bench/apps/frappe/frappe/popup/doctype/student/{state}"
os.makedirs(folder_path, exist_ok=True)
file_path = os.path.join(folder_path, file_name)
with open(file_path, "wb") as file:
file.write(file_content)
you can use a custom integration or library to transfer the file to the remote server after saving it locally. One popular library for transferring files to remote servers is paramiko .
import paramiko
from frappe.utils.file_manager import save_file
# default Frappe folder
file_doc = save_file(filedata, filename, dt, dn)
hostname = 'remote_server_ip'
username = 'remote_username'
password = 'remote_password'
remote_folder = '/path/to/remote/folder'
# Connect to the remote server using SSH
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname, username=username, password=password)
# Transfer the file to the remote server
sftp = ssh.open_sftp()
local_path = file_doc.file_url
remote_path = f"{remote_folder}/{filename}"
sftp.put(local_path, remote_path)
sftp.close()
ssh.close()
IF YOU HAVE NAS (Network Attached Storage) for Storage like Synology then
you can use various methods such as FTP, SFTP, or SCP. We can transfer files to a using the paramiko library and SFTP:
# Define the Synology NAS details
hostname = 'nas_ip_address'
port = 22 # Default SFTP port for Synology NAS
username = 'nas_username'
password = 'nas_password'
remote_folder = '/path/to/remote/folder'
By Help of this you can achieve some logic to any kind of doctype
Client Script
frappe.ui.form.on("YourDoctype", {
upload_file_button: function(frm) {
var file_input = document.createElement("input");
file_input.type = "file";
file_input.accept = "image/*"; // Specify the accepted file types here
file_input.onchange = function() {
var file = file_input.files[0];
var reader = new FileReader();
reader.onload = function(e) {
var filedata = e.target.result;
frappe.call({
method: "upload_file_to_remote_server",
args: {
filedata: filedata,
filename: file.name
},
callback: function(response) {
if (response.message) {
frappe.msgprint("File transferred successfully.");
} else {
frappe.msgprint("File transfer failed.");
}
}
});
};
reader.readAsDataURL(file);
};
file_input.click();
}
});