javascript - node.js download image with unknown extension from url -
my issue download image given link, there 1 trouble - dont know image extension(bmp, jpg, png etc.) using request module, , succesfully download image of png extension:
var fs = require('fs'); var request = require('request'); var url = 'www.images.com/image01.'; request(url+ '.png').pipe(fs.createwritestream('./image.png'));
but when dont know image extension. think maybe can check responce , if it's = '404' , second try download image from: url + '.[another extension]' , on. likes callback hell.
make request url, check content-type in header of request mime-type of file. can have file in project, called mime-types.js
where download file, can make request of file:
var mime = request('mime-types.js');
and content of mime-types.js file should this
const jpg = "image/jpeg"; module.exports = { jpg: jpg }
then can switch between different mime-types have , result of request.
you can find mime-types here: https://en.wikipedia.org/wiki/internet_media_type#type_image
Comments
Post a Comment