O'Reilly logo

PhantomJS Cookbook by Rob Friesel

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Saving a file from a PhantomJS script

Now we will introduce the fs module which provides an API for working with the filesystem from a PhantomJS script. In this recipe, we will demonstrate how to save a file from PhantomJS and use the separator property for generating filesystem-safe paths, checking whether the target directory exists, creating it if it does not, checking write permissions if it does, and then persisting the contents to the filesystem.

Getting ready

To run this recipe, we will need a script that expects to write a file to the filesystem. In order to write that file, we need write permissions in the destination directory.

The script in this recipe is available in the downloadable code repository as recipe08.js under chapter02. If ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required