Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

296
Views
In node.js, how to write binary data from fetch into a file?

Let's say I want to save an image to disk.

var path = require('path');
var webdriver = require("selenium-webdriver");
var chrome = require('selenium-webdriver/chrome');
var By = webdriver.By;
var until = webdriver.until
const fs = require("fs");
var o = new chrome.Options();
o.addArguments("--disable-web-security");
var browser = new webdriver.Builder().forBrowser('chrome').setChromeOptions(o).build();

fetch("https://upload.wikimedia.org/wikipedia/commons/9/9d/Blue_Flower.png");

This doesn't work, fetch is not defined.

I then tried adding fetch into browser.executeScript

var promise1 = browser.executeScript(`return fetch("https://upload.wikimedia.org/wikipedia/commons/9/9d/Blue_Flower.png");`)
promise1.then((x) => x.arrayBuffer()).then((buffer) => fs.writeFile("flower.png", buffer, () => console.log("done")))

This doesn't work. It turns out that when executeScript returns an object, all functions in the object are replaced with {}.

I then tried returning the buffer from executeScript.

var promise1 = browser.executeScript(`return fetch("https://upload.wikimedia.org/wikipedia/commons/9/9d/Blue_Flower.png").then((x) => x.arrayBuffer());`)
promise1.then((buffer) => fs.writeFile("flower.png", buffer, () => console.log("done")))

Still doesn't work. It turns out that buffer becomes {};

Is there some way I can access the arrayBuffer without it turning into {} after it comes out of executeScript?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

const res = await fetch(url);
const fileWriteStream = fs.createWriteStream('/directory/path.ext');
res.body.pipe(fileWriteStream);
about 4 years ago · Juan Pablo Isaza Report

0

I managed to do it by using base64. I used code from this answer :

var promise1 = browser.executeScript(`return fetch("https://upload.wikimedia.org/wikipedia/commons/9/9d/Blue_Flower.png").then((x) => x.arrayBuffer()).then(function(buffer){
    var blob = new Blob([buffer])

var reader = new FileReader();
reader.readAsDataURL(blob);
    
    return reader
}).then(function(reader){
    return new Promise(function(x, y){
        this.addEventListener("loadend", function(){
            x(this);
        }.bind(this))
    }.bind(reader)); 
}).then(function(reader){
    return reader.result.slice(reader.result.indexOf(",")+1);
})  
`)
promise1.then(function(b64data){
    let buff = Buffer.from(b64data, 'base64');
    fs.writeFileSync("flower.png", buff);
    console.log("done");
})
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!