Downloading a binary file and saving it #1720
-
I've been trying to write a downloaded binary file to a directory but either keep encountering errors or the file is incorrectly written differing from the source. This is within a Node environment and doesn't need to work in browsers. I've searched for hours regarding this topic but still without much luck. How would we modify the below example to successfully download the WOFF file and write it within the directory? const url = "https://fonts.gstatic.com/s/zillaslabhighlight/v10/gNMUW2BrTpK8-inLtBJgMMfbm6uNVDvRxiP0THTxaGR10A.woff2"
const dest = "./directory/file.woff2"
const options = {
encoding: "binary",
method: "GET",
}
try {
const response = await got(url, options).buffer()
fs.writeFileSync(dest, response)
} catch (error) {
console.log(error)
} Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
What errors are you getting? Don't forget that you also have to create the directory before writing if it doesn't already exist. Also, drop the |
Beta Was this translation helpful? Give feedback.
What errors are you getting?
Don't forget that you also have to create the directory before writing if it doesn't already exist. Also, drop the
encoding
option.