REST API Permission Error with 'upload_file' Method

Hello Frappe Community,

I am facing an issue with file uploads in the Job Application form of my Frappe application. Specifically, I am encountering a ‘PermissionError’ when attempting to use the ‘upload_file’ method.

Here’s what I’ve done so far:

  • Granted ‘Read’, ‘Write’, and ‘Create’ permissions to the “Job Applicant” role.
  • Tried adjusting role permissions in the Role Permissions Manager.

However, the error persists. Additionally, I have noticed that file uploads work correctly when a user is logged in, but the issue arises when a user is logged out.

Could anyone please guide me on the correct way to grant permissions for the ‘upload_file’ method? Any insights or experiences shared would be greatly appreciated.

let makecall = async()=>{
    let formdata = $('#jobapply').serializeArray().reduce(
        (obj, item)=>(obj[item.name]=item.value, obj), {}
    );
    let filedata = $('#jaFile')[0].files[0];
    // initialize form
    fileprivate = 1
    let fileContent = new FormData()
    if(filedata){
        fileContent.append('file', filedata);
        fileContent.append('is_private',fileprivate);
    }
    // end initialize

    // post to API
    if(formdata){
        let res = await $.ajax({
            url: '/api/resource/Job Applicant',
            type: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-Frappe-CSRF-Token': frappe.csrf_token
            },
            data: JSON.stringify(formdata),
            success: function(data){
                return data;
            },
            error: function(data){
                return data;
            }
        });
        console.log(res);
        // upload image
        if(res.data && filedata){
            let fileres = await fetch('/api/method/upload_file', {
                headers: {
                    'X-Frappe-CSRF-Token': frappe.csrf_token
                },
                method: 'POST',
                body: fileContent
            })
            .then(res=>res.json())
            .then(data=>{
                console.log(data);
                // finally update document
                if(data.message){
                    // update agent
                    $.ajax({
                        url: `/api/resource/Job Applicant/${res.data.name}`,
                        type: 'PUT',
                        headers: {
                            'Content-Type': 'application/json',
                            'X-Frappe-CSRF-Token': frappe.csrf_token
                        },
                        data: JSON.stringify({resume_attachment:data.message.file_url}),
                        success: function(data){
                            showPopup();
                            return data
                        },
                        error: function(data){
                            return data
                        }
                    })

                    // end update agent
                }
            })
        }
    }

}

error occur in this line

fetch('/api/method/upload_file'

Error:

{exception: 'frappe.exceptions.PermissionError', exc: '["Traceback (most recent call last):\\n  File \\"app…ssionError\\nfrappe.exceptions.PermissionError\\n"]'}

Thank you in advance for your help!

I have the same problem @NCP can you help with this

Hi everyone,

I wanted to follow up on my previous post regarding the PermissionError I was experiencing when guests tried to upload files using the ‘upload_file’ method in the Job Application form of my Frappe application.

Solution:
The issue has been successfully resolved by enabling the “Allow Guests to Upload Files” option under System Settings.

For those who might face a similar issue, here’s how you can enable this setting:

  1. Navigate to System Settings in your Frappe Dashboard.
  2. Look for the Permissions section.
  3. Find and check the option for Allow Guests to Upload Files.
  4. Save your changes.

This simple change has allowed file uploads to proceed without any permissions errors for guest users.

I hope this solution helps anyone who might be facing the same or similar issues. Thank you to everyone.

Best,
Masky

3 Likes