How to attach an existing file to a document

Here is the backend’s python code of the file manager:

This may help in an app.

There also is 1 whitelisted method in line 398ff.:

Finding this:

I see that there is an API endpoint in line 377 like this:
xhr.open('POST', '/api/method/upload_file', true);
(followed by some header an CSRF security items, then building a form upload structure)
which betrays a whitelisted upload_file method which might be of use.
It seems to be just one “grep -r upload_file *” away from a usable point in the frappe app tree.

Here is another code snippet showing how to upload a file (using shell):

And we even got a long list of many other v14 api endpoints useable for many purposes:

but, well, a recursive grep might help in many cases, too.