How to attach an existing file to a document

Maybe you could take a look at the recently added deprecation_dumpster, the intro from @blaggacao might indeed brighten everybody up as stated:

devx: add deprecation dumpster (#27887) · frappe/frappe@8cfeb15 · GitHub

Nice addition!

There you’ll even get a glimpse into upcoming changes for v17.

Found by skimming through the history of the handler.py file. Some code archeology helps to position yourself and understand where the developer ecosystem is coming from. This code also has its history which is interesting to understand how and why it eveolves in this or that direction. It’s even part of history in general, as it’s part of the search of most of us of how to live in a society (or sub-society) of people strongly driven by goodness and sharing in a situation of people with unusually strong attachment to freedom.

Anyhow, thanks so much @peterg and @Peer for your assistance in helping me with what this topic was all about form the outset : to attach a file from a backend .py script without uploading it first to create a File document. Your assistance is much appreciated and without knowledgeable forum members like yourselves this forum would in essence be nothing other than a wailing wall.

Generating arbitrary files on the backend is not documented because it’s not a feature of the Frappe Framework. It could potentially be added as a new feature, but people on these forums sometimes have strong opinions about new features. :slight_smile:

In any case, glad you have a solution. If you don’t actually need a file attached permanently, you could always generate it on the fly client-side. From what I know of your use case, that might actually be the cleanest solution.

function download_csv(content, mimeType, filename){
  const a = document.createElement('a') // Create "a" element
  const blob = new Blob([content], {type: mimeType}) // Create a blob (file-like object)
  const url = URL.createObjectURL(blob) // Create an object URL from blob
  a.setAttribute('href', url) // Set "a" element link
  a.setAttribute('download', filename) // Set download filename
  a.click() // Start downloading
}
const raw_csv = `
alice,1
bob,0
`
download_csv(raw_csv, 'text/txt', 'test.txt')

// stolen from stackoverflow

1 Like