-
-
Notifications
You must be signed in to change notification settings - Fork 21.7k
Closed
Labels
Description
NodeJS now has support for Blob's globally,
...earlier you had to load it from require('buffer').Blob
it would be cool / awesome if it where possible to respond with a Blob by doing something like
const str = `<h1>Hello World</h1>`
const blob = new Blob([str], { type: 'text/html' })
const file = new File([blob], 'index.html', { type: 'text/html' })
app.get('/', (req, res) => {
res.send(blob) // or:
res.download(blob, 'name.html')
res.download(file) // name taken from file instead
})Doing this would take care of
- Setting the response header
content-typeto the blob's type (only if content-type haven't been set manually) - Setting the response header
content-lengthto the blob's size - and pipe the data from
blob.stream()to the response - if you used
res.download(blob)then it would also add content-disposition attachment header
it's also looking like if node will at some point add a way of getting blobs from the filesystem also, but i don't know when.
ref: nodejs/node#39015
edit: NodeJS just shipped fs.openAsBlob(path, { type }) in v20+
LinusU, neysidev, chladnefazole, noe-lc, leegee and 7 more