Answer the question
In order to leave comments, you need to log in
How to organize image loading, previewing, sending and saving on the server in vue.js and node.js?
I use element-io
The task is this:
When uploading new images, you need to make a square thumbnail for the person to indicate it himself, then, when all the pictures also have thumbnails, send them to the server, save them in a folder, write the links to the database.
What is now:
<el-upload
id="elImageAddNew"
drag
action=""
:auto-upload="false"
:on-success="ElImageSuccess"
:before-upload="beforeElImageUpload"
:show-file-list="false"
:on-change="handleChangeIMG"
multiple
:file-list="ElImageForm.preImagesArray"
class="dialog-add-image-content">
<i class="el-icon-upload"></i>
<div class="el-upload__text">Перенесите файлы или <em>нажмите для загрузки</em></div>
<p>JPG, GIF или PNG.</p>
</el-upload>
handleChangeIMG (file, fileList) {
console.log('handleChange', file, fileList)
this.ElImageForm.preImagesArray.push(URL.createObjectURL(file.raw))
}
this.$refs.cropper[0].getCropBlob(data => {
// делаем изображения из зоны выбора миниатюры
let imgMini = window.URL.createObjectURL(data)
//создаём объект для добавления в итоговый массив
let ob = {
img: that.ElImageForm.preImagesArray[index],
img_mini: imgMini
}
// Добавляем в итоговый массив
that.ElImageForm.el_src.push(ob)
// Если последняя миниатюра, то надо отправить на сервер
if (!hasIndex) {
that.ElImageSendAddImages()
}
})
Api().post('settings', params)
// feed - это переданный params
if (feed.value_type === 'image') {
// save file
// save in db
if (Array.isArray(feed.new_value)) {
... тут всё отрабатывает нормально
}
}
Answer the question
In order to leave comments, you need to log in
I'll clarify the question (perhaps there is an answer in it): are you asking for a "piece of code" to save files in the folder ?
more or less like this:
const receiveFile = (filepath, req, res) => {
const wstream = fs.createWriteStream(filepath, {flags: 'wx'});
req
.pipe(wstream)
.on('error', (e) => {
if (e.code === 'EEXIST') {
res.statusCode = 409;
res.end('File Already Exists');
return;
}
res.statusCode = 500;
res.end('Internal Server Error');
})
.on('close', () => {
res.statusCode = 201;
res.end('File Created');
});
res.on('close', () => {
if (res.finished) return;
fs.unlink(filepath, (err) => {});
});
};
try via multer ( https://github.com/expressjs/multer). I save files in /uploads and put their names in the database
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question