Deno: Writing blob to local file.

Created on 19 May 2020  路  3Comments  路  Source: denoland/deno

Hi,
I'm fetching a zip file form a url and I want to save the blob into a local file:

fetch(downloadUrl, {
            method: 'GET'
        })
        .then(response => response.blob())
        .then(blob => {
            console.log(blob.size)
            // Save the blob to a file
        })

any idea on how to save the blob?

Many thanks.

Most helpful comment

@fakoua I am not sure if your question was answered.
Just adding the solution, so if someone comes across, he can find the answer.

fetch(downloadUrl, {
            method: 'GET'
        })
        .then(response => response.blob())
        .then(blob => {
             const buffer = await blob.arrayBuffer();
             const unit8arr = new Deno.Buffer(buffer).bytes();
             Deno.writeFileSync("path/to/file", unit8arr);
        })

All 3 comments

await Deno.writeFile("path/to/file", await blob.arrayBuffer())

Please use the discord first for help and questions: https://discord.gg/deno.

Thank you very much,

The provided solution does not work, it says: ArrayBuffer is not assignable to Uint8Array,
Anyway, i will switch to discord,

thank you again.

@fakoua I am not sure if your question was answered.
Just adding the solution, so if someone comes across, he can find the answer.

fetch(downloadUrl, {
            method: 'GET'
        })
        .then(response => response.blob())
        .then(blob => {
             const buffer = await blob.arrayBuffer();
             const unit8arr = new Deno.Buffer(buffer).bytes();
             Deno.writeFileSync("path/to/file", unit8arr);
        })

Was this page helpful?
0 / 5 - 0 ratings

Related issues

xueqingxiao picture xueqingxiao  路  3Comments

ry picture ry  路  3Comments

somombo picture somombo  路  3Comments

sh7dm picture sh7dm  路  3Comments

kitsonk picture kitsonk  路  3Comments