Can we configure the allowed file extensions for a field Attach ?
I’d like to limit to only .xml files.
this should be doable as a custom app.
hook to file - validate.
in the method check the filename.
Sure I can validate, but I want the file browser to default to *.xml instead of *.*
And I think this should be built in frappe, I’ll submit a feature request on github.
You can achieve this via the following Client Script:
frappe.ui.form.on("MY DOCTYPE", {
refresh: function (frm) {
const attach_field = frm.fields_dict["MY FIELDNAME"];
attach_field.on_attach_click = function () {
attach_field.set_upload_options();
attach_field.upload_options.restrictions.allowed_file_types = [
"application/xml",
"text/xml",
];
attach_field.file_uploader = new frappe.ui.FileUploader(attach_field.upload_options);
};
}
});
Tested on develop
/ v16.
I really need to this with child doctype , do you have a way to do that please?
I have the same requirements as you.
After seeking help from DeepWiki, I managed to find a solution.
In the parent doctype client script (mine is called manufacturing_order.js
), use a code like this:
function limitMimeTypesForDesignFiles(frm) {
const grid = frm.get_field('manufacturing_items').grid;
for(let i = 0; i < grid.grid_rows.length; i++) {
const grid_row = grid.get_row(i);
grid_row.toggle_editable_row(true);
const designFilesField = grid_row.on_grid_fields_dict['design_files'];
designFilesField.on_attach_click = function () {
designFilesField.set_upload_options();
designFilesField.upload_options.restrictions.allowed_file_types = [
// "application/dxf",
// "application/sldprt",
"application/xml",
"text/xml"
];
designFilesField.file_uploader = new frappe.ui.FileUploader(designFilesField.upload_options);
};
grid_row.toggle_editable_row(false);
}
}
frappe.ui.form.on("Manufacturing Item", {
manufacturing_items_add: limitMimeTypesForDesignFiles,
manufacturing_items_remove: limitMimeTypesForDesignFiles
});
frappe.ui.form.on("Manufacturing Order", {
refresh(frm) {
limitMimeTypesForDesignFiles(frm);
}
});
In the previous code, the parent doctype is Manufacturing Order
, the child is Manufacturing Item
, the table field is manufacturing_items
and inside it, the attach file field is design_files
.
The code iterates over all rows on form load and also after every row added and removed, it finds the attachment field after making the row editable (very important, if not editable it will not work), then do the same logic presented by @rmeyer
In the previous answer, I considered only the embedded table view, like what is in this screenshot:
But I missed the popup view, like this:
So, I am updating the previous code to work on both views. Here is the updated version of manufacturing_order.js
:
// Copyright (c) 2025, CRTI - SMRIS and contributors
// For license information, please see license.txt
function setUpMimeTypesAttachment(attachmentField, allowedMimeTypes) {
attachmentField.on_attach_click = function () {
attachmentField.set_upload_options();
attachmentField.upload_options.restrictions.allowed_file_types = allowedMimeTypes;
attachmentField.file_uploader = new frappe.ui.FileUploader(attachmentField.upload_options);
};
}
function modifyGridForSelectedMimeTypes(frm) {
const grid = frm.get_field('manufacturing_items').grid;
for(let i = 0; i < grid.grid_rows.length; i++) {
const grid_row = grid.get_row(i);
grid_row.toggle_editable_row(true);
const designFilesField = grid_row.on_grid_fields_dict['design_files'];
setUpMimeTypesAttachment(designFilesField, [
"application/xml",
"text/xml"
]);
grid_row.toggle_editable_row(false);
}
}
frappe.ui.form.on("Manufacturing Item", {
manufacturing_items_add: modifyGridForSelectedMimeTypes,
manufacturing_items_remove: modifyGridForSelectedMimeTypes,
form_render(frm, cdt, cdn) {
const open_grid_form = frappe.ui.form.get_open_grid_form();
const designFilesField = open_grid_form.grid_form.fields_dict["design_files"];
setUpMimeTypesAttachment(designFilesField, [
"application/xml",
"text/xml"
]);
}
});
frappe.ui.form.on("Manufacturing Order", {
refresh(frm) {
modifyGridForSelectedMimeTypes(frm);
}
});
Notice the new form_render
logic in Manufacturing Item
.