javascript - 如何在电子应用程序中本地保存图像
问题描述
我正在使用vue-cli-electron-builder 构建一个电子应用程序。
用户提交包含姓名、电子邮件和图像(照片)的表单,这些表单存储在本地 mysql 数据库中,稍后将同步到云端。只有图像名称将存储在数据库中,问题是
- 我在哪里以及如何在本地保存实际图像?
- 如何将图像同步到云端?
在开发模式下,它存储在项目中,但生产捆绑的应用程序不能那样工作。
我尝试 multer.diskStorage 将图像保存到“./server/uploads”。它在开发模式下工作,但在生产中它不起作用。
解决方案
您可以将上传的内容存储在userAppData
文件夹中。你应该尝试如下
function uploadFile() {
dialog.showOpenDialog({
properties: ['openFile','multiSelections'],
filters: [{
name: 'Images',
extensions: ['jpg', 'png', 'gif']
}]
},
uploadF, //define callback
)
}
function uploadF(filePaths) {
if (filePaths!=undefined) {
//multiple image upload
for (let i = 0; i < filePaths.length; i++) {
let fileName = path.basename(filePaths[i])
fileName = moment().unix()+'-'+fileName //rename file
let fileUploadPath = app.getPath('userData')+ '' + fileName;
move(filePaths[i],fileUploadPath,cb)
}
}
}
function cb(e) {
console.log("error in upload file",e);
}
function move(oldPath, newPath, callback) {
fs.rename(oldPath, newPath, function (err) {
if (err) {
if (err.code === 'EXDEV') {
copy();
} else {
console.log("err",err);
callback(err);
}
return;
}
callback();
});
function copy() {
var readStream = fs.createReadStream(oldPath);
var writeStream = fs.createWriteStream(newPath);
readStream.on('error', callback);
writeStream.on('error', callback);
readStream.on('close', function () {
let fileName=path.basename(newPath)
console.log(fileName," file uploaded")
//remove path from destination
//fs.unlink(oldPath, callback);
// do your stuff
});
readStream.pipe(writeStream);
}
}
推荐阅读
- html - 右下对齐空格键
- corda - 使用 Corda RPC 启动流程
- java - 在临时 Spring Boot 项目中使用 ArrayList 作为数据源
- laravel - 在 Laravel 中使用 updateOrCreate() 方法时我应该使用哪个 HTTP 请求?
- php - PHP OOP 更新扩展类 __construct 上的受保护字符串
- python - 问题 #24748:跳过 _load_module_shim 中的 sys.modules 检查;总是加载新的扩展
- c++ - 无法在 openGL 和 GLUT 中为我想要的确切对象设置动画
- list - 使用foreach,将每个元素添加到列表而不是编写,Prolog
- php - 我可以让我的 php 页面重定向带有错误消息的自定义 html 页面吗
- python - 使用 PIL 调整文件夹中所有图像的大小