0

I am using protractor.I have to download some no.of images and upload that to corresponding locations and each of the images are different.I have used below code for this

            const download = require('image-downloader');
                 options = {
                       url: 'https://dbamedia.blob.core.windows.net/files/34f6fbc070754541a8828bb78c6a1e87',                     
                       dest: './files'        
                   }            
                   download.image(options).then(({ filename, image }) => {
                     console.log('File saved to', filename)
                   }).catch((err) => {
                   console.error(err)
                   });

I am able to download the image by this code.but when i am checking the destination folder, file name not having the extensions(.jpg,.png).So that when i am trying to upload these files, i am getting an error 'file is not supporting' .If its downloaded with the file extension, i am able to upload it successfully.How can i solve this.Thanks in advance.

Devleena
  • 453
  • 9
  • 25

2 Answers2

0

To save file with another name you need to add filename in the dest property:

const download = require('image-downloader');
options = {
  url: 'https://dbamedia.blob.core.windows.net/files/34f6fbc070754541a8828bb78c6a1e87',                     
  dest: './files/image.jpg'  // Save file as image.jpg in the ./files      
}   

download.image(options).then(({ filename, image }) => {
  console.log('File saved to', filename)
}).catch((err) => {
  console.error(err)
});
Yevhen Laichenkov
  • 7,746
  • 2
  • 27
  • 33
0

When you are able to download the image then why not read the downloaded file extension first and then append it in filename instead of hardcode .jpg in @Yevhen answer.

To read the downloaded file name extension you can refer this SO question How can I get file extensions with JavaScript?

Ajitesh
  • 16
  • 2
  • As per my image url, its file extension is not available when i am downloading it.So i think, i need to rename it – Devleena Jul 30 '19 at 05:12