我有一系列远程文件地址。我只是使用 for in 到 foreach 数组和 foreach 的正文中,我开始 HTTP GET 请求以进行数据下载。但一切都是异步的,我需要知道文件名才能将文件保存在请求回调中。
解决此问题的最佳做法是什么?
演示代码:
files = ["url.com/file.png", "url.com/file.doc"]
for file in files
req = http.get file, (response) =>
response.setEncoding 'binary'
body = ""
response.on "data", (chunk) =>
body += chunk
response.on "end", () =>
#Here I needs to know the file name to save it
fs.writeFileSync @currentFolder + "/Files/" + file, body, "binary"
谢谢!