Chris Rutherford
Chris Rutherford

Reputation: 1672

fs module writeFileStream not actually creating file on disk

working on a small file transfer utility to replace an old email based system for order processing, and I'm using Nodejs, express, and a host of other libraries for this.

The current issue is that I have the data pulled over just fine, but I can't seem to actually save the file to disk at the end.

var file_url = `${config.poll.transUrl}/?location=${config.location}&transmission=${config.poll.transmission}`;
console.log(file_url);
var download_path = config.poll.folder;
var filename = setFileName();
var fileStream = fs.createWriteStream(download_path + filename);
fileStream.on('finish', ()=>{
  console.log(`${filename} has been downloaded to: ${download_path}`);
});
http.get(file_url, (res)=>{
  res.on('data', (data)=>{
    console.log(data.toString());
    fileStream.write(data);
  })
  .on('end',()=>{
    fileStream.close();
    fileStream.end();
  });
});

Here's the code that I've been using, and it's just a snippet. Assume that all variables are set and are of the correct type, as I've ensured that is the case here.

According to what I understand, the fileStream.end() function is supposed to close the stream and save the file to disk, but it doesn't do so. I look in the folder where it's supposed to be, and nothing.

Also for more information, here is my config object:

module.exports = { location: 'CA', watch:{ folder: './watch/', transUrl: 'http://localhost:3289', transmission: 'send' }, poll:{ folder: './recieve', transUrl: 'http://localhost:3289', transmission: 'receive' } }

Upvotes: 0

Views: 282

Answers (2)

idbehold
idbehold

Reputation: 17168

The correct way to do this is with pipe:

http.get(file_url, (res) => {
  const filePath = path.join(download_path, filename)
  const writeStream = fs.createWriteStream(filePath)
  res.pipe(writeStream)
    .on('error', (e) => console.error(e))
    .on('close', () => console.log(`file was saved to ${filePath}`))
})

Upvotes: 1

Chris Rutherford
Chris Rutherford

Reputation: 1672

Eventually found a workaround:

Final code ignores the concept of adding to the stream as the data comes in as the data is purely text in the current implementation.

Final code is as follows:

  var file_url = `${config.poll.transUrl}/?location=${config.location}&transmission=${config.poll.transmission}`;
console.log(file_url);
var download_path = config.poll.folder;
var fileContent = '';
var filename = setFileName();
var fileStream = fs.createWriteStream(download_path + filename);
fileStream.on('finish', ()=>{
  console.log(`${filename} has been downloaded to: ${download_path}`);
});
http.get(file_url, (res)=>{
  res.on('data', (data)=>{
    fileContent += data.toString();
  })
  .on('end',()=>{
    fs.writeFile(path.join(download_path, filename), fileContent,(err) =>{
      if(err){
        return console.error(err);
      }
      console.log('file was saved')
    })
  });
});

Upvotes: 0

Related Questions