Custom directory path for files uploaded via "attach" field type

In a custom doctype, I have “attach” field types to upload specific documents. I want those documents to be stored in a specific place in the file manager. Is there a way to edit the “attach” field type to have it that way. Thank You.

1 Like

Does anyone have an update on this?

I have the Same Problem. Please help us. I am proving the below link.

Thank you.

1 Like

Is there any solution ?

Thanks and Regards
Akshay

No I have the same question ?
If someone from the frappe team could answer

This could work…

  • add an on_update hook for File (in hooks.py)
  • check if it is attached to your custom doctype, if so move it to your required folder

@vijaywm Thanks, man! Did you find any alternative ways?

No, not tried.

I have used the below code to solve the problem:


@frappe.whitelist()
def capture(file=None):
    result , response_api = image_api(file)
    if result:


        docname = frappe.get_request_header("Referer").split("/")[-1]
        print(f"{file}")
        file_content = frappe.local.uploaded_file
        file_name_old = frappe.local.uploaded_filename
        

        
        folder_path = f"/home/frappe_dev/frappe-bench/sites/developement.expd/public/files"
        os.makedirs(folder_path, exist_ok=True)

        file_path = os.path.join(folder_path, file_name_old)


        with open(file_path, "wb") as file:
            file.write(file_content)
            
        host = frappe.request.host

        # Check if the port is needed (adjust according to your environment)
        port = ":8000" if "localhost" in host or "127.0.0.1" in host else ""

        # Construct the dynamic URL
        sys_file_path = f"http://{host}{port}/files/{file_name_old}"
        email_id = frappe.session.data.get('user')

        file_doc = frappe.new_doc("File")
        file_doc.name = "random"
        file_doc.file_name = file_name_old
        file_doc.file_url = sys_file_path
        file_doc.attached_to_name = docname
        file_doc.folder = "Home"
        file_doc.attached_to_doctype = "Face Detection passport"
        file_doc.attached_to_field = 'upload_image'



        file_doc.insert()

        frappe.db.set_value("File", file_doc.name, "file_url", f"{sys_file_path}")

    

    return str(response_api.json())
frappe.ui.form.on("Face Detection passport", {
	upload_image: function (frm) {
        
        if (frm.doc.__islocal) {
            frm.save().then(() => {
                uploadFile(frm);
            });
        } else {
            uploadFile(frm);
        }
    }
    
});

function uploadFile(frm) {
    new frappe.ui.FileUploader({
        method: 'frappe.image_detection.doctype.face_detection_passport.face_detection_passport.capture',
        make_attachments_public: "False",
        dialog_title: "Upload Files",
        disable_file_browser: "False",
        allow_link: "True",
        frm: frm,
        on_success(file, response) {
            frappe.msgprint(`${"Server response:"+response.message}`, "Message");  // Log the response for debugging

            frappe.show_alert({
                message: __('File uploaded.'),
                indicator: 'green'
            }, 5);

            frm.refresh();
        }
    });
}