Skip to content

Instantly share code, notes, and snippets.

@youqingkui
Last active August 29, 2015 14:27
Show Gist options
  • Save youqingkui/5e2cf967c56311f9cd95 to your computer and use it in GitHub Desktop.
Save youqingkui/5e2cf967c56311f9cd95 to your computer and use it in GitHub Desktop.
node保存远程图片到本地
async.waterfall [
(callback) ->
op = {
url:self.url
encoding:'binary'
}
request.get op, (err, res, body) ->
return console.log err if err
mimeType = res.headers['content-type']
mimeType = mimeType.split(';')[0]
image = new Buffer(body, 'binary')
callback(null, image, mimeType)
(image, mimeType, callback) ->
baseDir = __dirname + '/static'
checkTime = getLocalTime(self.deviceInfo.check_time)
locationTime = getLocalTime(self.deviceInfo.location_time)
fileRes = baseDir + '/' + checkTime + '/' + locationTime + MIME_TO_EXTESION_MAPPING[mimeType]
dir = baseDir + '/' + checkTime
if !fs.existsSync(dir)
fs.mkdirSync(dir)
fs.writeFileSync fileRes, image
console.log "write ok"
]
# https://app.yinxiang.com/shard/s5/nl/341662775/e5ee0420-ee00-4221-a8d3-078b176ed318/
# https://app.yinxiang.com/shard/s5/nl/341662775/c8a364f2-b0a2-4ebf-b466-4749b652b1e1/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment