Если вы хотите сохранить изображение на сервере, ваш сервер должен загрузить файл и сохранить его.
Вот простой пример того, как я бы это сделал:
const path = require('path')
const fs = require('fs')
const URL = require('url').URL
const http = require('http')
const https = require('https')
// Define a function to download images from a passed array of URL's.
const downloadImages = (arr, folderpath = '') => {
// - Create a new array of `Promise`s from the passed array, using `Array.map`
// and pass that new array to `Promise.all` so they are all waited for
// in-parallel.
// - Each `Promise` in this new array resolves when the data specified in
// the passed array `item.URL` is downloaded and saved to a file with the
// appropriate name.
return Promise.all(arr.map(item => {
// For each item in the passed array...
// Create and return a Promise which:
return new Promise((resolve, reject) => {
// Creates a `WriteStream` to a local file on path:
// '[root directory path]/[folderpath]/[item.title].[extension]'
const extension = path.extname(item.url)
const filepath = `${path.dirname(process.mainModule.filename)}${folderpath}/${item.title}${extension}`
const writeStream = fs.createWriteStream(filepath)
// Reject if there's an error acquiring or writing to the `WriteStream`.
writeStream.on('error', reject)
// - Ensure we use the appropriate http module for downloading the file.
// - If the `item.url` is HTTPS we use the `https` module, otherwise we
// use the `http` module.
const protocol = new URL(item.url).protocol
const requester = protocol === 'https:' ? https : http
// Get a stream to the data on the URL.
requester
.get(item.url, res => {
// Reject the Promise if the HTTP status code is not 200.
if (res.statusCode !== 200) {
return reject(`${item.url} failed, HTTP status: ${res.statusCode}`)
}
// Otherwise pass the stream to the `WriteStream` so it can be written
// to the local file.
res.pipe(writeStream)
// When it's done, resolve the Promise.
writeStream.on('finish', resolve)
})
.on('error', reject) // Also reject if there's a generic request error.
})
}))
}
// Test the function in an async IIFE since we are going to use await.
;(async () => {
try {
// `downloadImages` returns a Promise so we need to await it.
await downloadImages([
{
title: 'lenna-color',
url: 'https://upload.wikimedia.org/wikipedia/en/7/7d/Lenna_%28test_image%29.png'
},
{
title: 'lenna-grayscale',
url: 'https://www.ece.rice.edu/~wakin/images/lenaTest3.jpg'
}
])
console.log('done')
} catch (err) {
console.error(err)
}
})()
Вас интересует функция downloadImages(arr, folderpath)
, которую я определил выше.
Если вы хотите сохранить изображения в корневой каталог, вы вызываете его так:
await downloadImages([
{
title: 'lenna-color',
url: 'https://upload.wikimedia.org/wikipedia/en/7/7d/Lenna_%28test_image%29.png'
},
{
title: 'lenna-grayscale',
url: 'https://www.ece.rice.edu/~wakin/images/lenaTest3.jpg'
}
])
или сохранить в папке с именем «images», которая существует в корневом каталоге:
await downloadImages([
{
title: 'lenna-color',
url: 'https://upload.wikimedia.org/wikipedia/en/7/7d/Lenna_%28test_image%29.png'
},
{
title: 'lenna-grayscale',
url: 'https://www.ece.rice.edu/~wakin/images/lenaTest3.jpg'
}
], '/images')
person
nicholaswmin
schedule
29.11.2019