I want to get the suffix name of this link before fs.createWriteStream
Then pipe it to this data stream to save the file.
I have tried the Promise method, and then there is a complete response, which cannot be written to the data stream.
And I have also tried the fs.writeFile method, but the writing format is incorrect.
Note: There is no file extension in the URL
The following is the code
const fs = require('fs');
const Promise = require('bluebird');
const request = require('request');
let url = 'http://mmbiz.qpic.cn/mmbiz_jpg/D1Wza369eswnoapNaAdvtqygvMAnViaCLa8AMwwDZ4rebKrPvicxFf8zuibfialkPD3A7w8omWjicVm7GhFWcsibfGibw/0';
let file = fs.createWriteStream('file');
request
.get(url)
.on('response', function (response) {
let type = response.headers['content-type']
type = '.' + type.substring(type.lastIndexOf('/') + 1);
console.log(type);
// 在这里加是不可以的
})
.pipe(file)
.on('end', function () {
console.log('end')
})
闭关修行中......