Upload file API calls

Hi all,

I have a question regarding File Uploads API, original document: REST API.

  1. I am not sure how to call curl in javascript. After googling it, jquery - Perform curl request in javascript? - Stack Overflow 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);

3 Likes

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