我使用的第三方API可以给我多个图像。
1)首先,我需要将这些图像下载到服务器上,并对所有图像进行ZIP处理。(第三方API提供了图像URL)
2)成功创建Zip后,用户将在其本地系统上下载该ZIP。
我需要使用nodejs或其NPM库的
执行此任务请给我一些连击方法来执行此操作
答案 0 :(得分:0)
import cmd from 'node-command-line'
import zipFolder from 'zip-folder'
import Download from 'image-downloader'
首先创建目录或文件夹以将其转换为zip
const zipName = `zipFolderName`
//Folder name which become zip
cmd.run(`mkdir -p ${__dirname}/../public/`)
//Create public direactory in server(backend)
cmd.run(`chmod -R 777 ${__dirname}/../public/`)
//Give persmission on public folder
cmd.run(`mkdir -p ${__dirname}/../public/${zipName}/`)
cmd.run(`mkdir -p ${__dirname}/../public/${zipName}/${zipName}/`)
使用url下载所有图像或文件并存储
const promiseArr = []
const url = decodeURI('path_of_url') //Decode url
const fileName = url.replace(/.*\//g, '') //extract filename from url
const options = {
url: urls[i],
dest: `${__dirname}/../public/${zipName}/${zipName}/${fileName}`
//Path of your zip file
}
Download.image(options)
.then((filename) => {
promiseArr.push(filename) //If we want to return promise
})
sjkhderhkjwe
zipFolder(`${__dirname}/../public/${zipName}/`, //path of folder
`${__dirname}/../public/${zipName}.zip`, //where zip made
async (err) => {
if (err) {
thorw new Error(err)
} else {
const urls = `${__dirname}/../public/${zipName}.zip`
//const zipUrl = await uploadZipToAWS(zipName, urls)
// Upload zip or move zip to front End
cmd.run(`rm -rf ${__dirname}/../public/${zipName}/`)
//Remove this zip from server
cmd.run(`rm -rf ${__dirname}/../public/${zipName}.zip`)
//Remove this zip from server
//Return back url
}
})
请注意Path(__ dirname)。 在服务器端安装模块。并根据您的要求管理代码。
在前端发送回zip的url(路径),并在浏览器中进行访问或下载。