Try ERPNext Buy Support Partners Foundation

Upload file API calls

Hi all,

I have a question regarding File Uploads API, original document: https://frappeframework.com/docs/user/en/api/rest#file-uploads.

  1. I am not sure how to call curl in javascript. After googling it, https://stackoverflow.com/questions/25515936/perform-curl-request-in-javascript gives the result of how to call curl. then the question is:
    -H in the documentation is the header, what about -F? do I use xhr.send(file);?
  2. the documentation states that we need to use: -H ‘Authorization: token xxxx:yyyy’, how do I access this token data?
    I thought api/method/frappe.auth.get_logged_user would return the token for me, but it only returns the user name for me.
  3. Or is there any way of upload the file?

thanks in advance.

I just saw the code, xhr.setRequestHeader(‘X-Frappe-CSRF-Token’, frappe.csrf_token); should be good for token.

I just figured it out by trying it.

xhr.open(‘POST’, ‘/api/method/upload_file’, true);

xhr.setRequestHeader(‘Accept’, ‘application/json’);

xhr.setRequestHeader(‘X-Frappe-CSRF-Token’, frappe.csrf_token);

let form_data = new FormData();

var file = document.getElementById(‘id of the input file’).files[0];

form_data.append(‘file’, file, file.name);

xhr.send(form_data);

How were you able to “move” the file ? I mean it’s only uploaded then, but not attached to any docs?

To anyone wondering - the example already specifies how its done. Kinda the same of what you can see when looking into the network debugger of your browser while uploading.

Thanks for your reply