node- 图片文件下载存储 拷贝

import { defHttp } from './src/utils/http/axios';

// const fs = require('fs-extra');
import fs from "fs-extra"

// const https = require('https');
// const axios = require('axios');
// const defHttp = require('/@/utils/http/axios');

// downLoadThemeSetting();
function downLoadThemeSetting() {
axios
.get('xxxx')
.then((response) => {
// 请求成功处理
console.log(response.data);
})
.catch((error) => {
// 请求失败处理
console.error(error);
});

// let data = await getSysTheme(null);
// if (data.logoUrl) {
// console.log('App系统设置:', data.logoUrl);
// loadAndCopy(data.logoUrl, './temp/test', 'logo');
// }
}
const homeUrl = 'xxx';
const logoUrl = 'xxxx';

// loadAndCopy(logoUrl, './temp/public', 'logo', './public');
// loadAndCopy(logoUrl, './temp/test1', 'jeecg_logo', './src/assets/icon');
// loadAndCopy(homeUrl, './temp/test2', 'jeecg_bg', './src/assets/icon');

const getSysTheme = (params) => defHttp.get({ url: '/xxxxxx', params });

function loadAndCopy(url, pathDir, imgName, toPath) {
https.get(url, function (res) {
let imgData = '';
res.setEncoding('binary'); //一定要设置response的编码为binary否则会下载下来的图片打不开
res.on('data', function (chunk) {
imgData += chunk;
});
res.on('end', function () {
if (imgData) {
console.log('下载成功!');
let folder = pathDir; //'./temp/image';
let path = folder + '/' + imgName + '.png'; //loginLogo
console.log(path);
if (!fs.existsSync(folder)) {
console.log('文件夹不存在,去创建');
fs.mkdir(folder, { recursive: false }, function (err) {
console.log('文件夹创建成功, 存储文件', err);
fs.writeFile(path, imgData, 'binary', function (err) {
if (err) {
console.log('down fail', err);
} else {
console.log('down success');
updateAssetFile(folder, toPath);
}
});
});
} else {
console.log('文件夹存在,存储更新文件');
fs.writeFile(path, imgData, 'binary', function (err) {
if (err) {
console.log('down fail', err);
} else {
console.log('down success');
updateAssetFile(folder, toPath);
}
});
}
} else {
console.log('下载失败!,图片路径不存在!');
}
});
});
}

function updateAssetFile(filePath, toPath) {
fs.copySync(filePath, toPath);
// if (filePath.indexOf('public') != -1) {
// fs.copySync(filePath, './public');
// } else {
// fs.copySync(filePath, './src/assets/loginmini');
// }
console.log('图片已成功覆盖到src文件夹下。');
}

  • 25
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值