node+vue實(shí)現(xiàn)文件上傳功能
本文實(shí)例為大家分享了node+vue實(shí)現(xiàn)文件上傳的具體代碼,供大家參考,具體內(nèi)容如下
*后端*
const express = require('express');
const Router = express.Router();
const multer = require('multer');
const fs = require('fs');
const pathLib = require('path');
const videoModel = require('../../models/my_yx_app/video');
//設(shè)置 視頻文件存放位置
const uploadVido = multer({
dest:'uploads_yx_app/video/'
});
//上傳視頻
Router.post('/uploadVideo',uploadVido.single('file'),(req,res)=>{
if (req.file) {
let file = req.file;
let newName = file.path+pathLib.parse(file.originalname).ext; //修改path
fs.rename(file.path,newName,(err)=>{ //修改path
if (err) {
return res.status(200).json({
code:0,
msg:'服務(wù)器繁忙!'
})
}else {
return res.status(200).json({
code:1,
msg:'上傳完成',
title:pathLib.parse(file.originalname).name,
videoUrl:'http://127.0.0.1:3001/uploads_yx_app/video/'+file.filename+pathLib.parse(file.originalname).ext
})
}
})
}else {
return res.status(200).json({
code:0,
msg:'服務(wù)器繁忙!'
})
}
});
*前端*
<div class="from-contral" style="position: relative">
<!--此處name 與 uploadVido.single('file') 相同-->
<input type="file" name="file" @change="changeFile" class="customStyle">
<el-button size="mini" type="primary">
添加文件
<i class="el-icon-upload el-icon--right"></i>
</el-button>
<div class="zt-title-video">{{ file.name }}</div>
<div class="zt-progress" v-show="percentageShow">
<el-progress
:text-inside="true"
:stroke-width="20"
:percentage="percentage"
>
</el-progress>
</div>
</div>
*js處理邏輯數(shù)據(jù)*
saveData() { //上傳
let that = this;
let fd = new FormData();
fd.append('file', this.file);
fileUpdata({ //上傳文件存儲(chǔ)在后端
method: 'post',
url: '/uploadVideo',
data: fd,
//監(jiān)聽(tīng)上傳時(shí)間 //實(shí)現(xiàn)進(jìn)度條
onUploadProgress(progressEvent) {
that.percentageShow = true;
that.percentage = parseInt(((progressEvent.loaded / progressEvent.total) * 100));
}
}).then(res => {
if (res.data.code === 1) {
//數(shù)據(jù)持久化
fileUpdata({
method: 'post',
url: '/saveVideoInfo',
data: {
videoUrl: res.data.videoUrl, //路徑
videoName: res.data.title, //標(biāo)題
videoType:that.videoType, //類(lèi)型
userName:localStorage.getItem('username') //那個(gè)用戶(hù)上傳的
}
}).then(res => {
if (res.data.code === 1) {
setTimeout(function () { //為什么延遲,為了使進(jìn)度條走完
that.$message({
message: '上傳成功',
type: 'success'
})
}, 1000);
}
}).catch(err => {
this.$message.error('服務(wù)器繁忙,請(qǐng)稍后重試!');
});
}
}).catch(err => {
this.$message.error('服務(wù)器繁忙,請(qǐng)稍后重試!');
})
},
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- springboot整合vue實(shí)現(xiàn)上傳下載文件
- vue實(shí)現(xiàn)文件上傳功能
- vue實(shí)現(xiàn)文件上傳讀取及下載功能
- vue.js異步上傳文件前后端實(shí)現(xiàn)代碼
- nodejs multer實(shí)現(xiàn)文件上傳與下載
- nodejs+express實(shí)現(xiàn)文件上傳下載管理網(wǎng)站
- NodeJS使用formidable實(shí)現(xiàn)文件上傳
- Vue實(shí)現(xiàn)附件上傳功能
- 使用Vue實(shí)現(xiàn)圖片上傳的三種方式
- Vue axios 中提交表單數(shù)據(jù)(含上傳文件)
相關(guān)文章
node文件上傳功能簡(jiǎn)易實(shí)現(xiàn)代碼
本篇文章主要介紹了node文件上傳功能簡(jiǎn)易實(shí)現(xiàn)代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06
Nodejs從有門(mén)道無(wú)門(mén)菜鳥(niǎo)起飛必看教程
下面小編就為大家?guī)?lái)一篇Nodejs從有門(mén)道無(wú)門(mén)菜鳥(niǎo)起飛必看教程。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-07-07
k8s node節(jié)點(diǎn)重新加入master集群的實(shí)現(xiàn)
這篇文章主要介紹了k8s node節(jié)點(diǎn)重新加入master集群的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
手寫(xiě)簡(jiǎn)版無(wú)三方依賴(lài)的Node-Server實(shí)現(xiàn)示例
這篇文章主要為大家介紹了手寫(xiě)簡(jiǎn)版無(wú)三方依賴(lài)的Node-Server實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
pm2與Verdaccio搭建私有npm庫(kù)過(guò)程詳解
這篇文章主要介紹了pm2與Verdaccio搭建私有npm庫(kù)過(guò)程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
nodejs基礎(chǔ)之多進(jìn)程實(shí)例詳解
這篇文章主要介紹了nodejs基礎(chǔ)之多進(jìn)程,結(jié)合實(shí)例形式分析了nodejs多進(jìn)程的概念、原理、相關(guān)函數(shù)使用方法及操作注意事項(xiàng),需要的朋友可以參考下2018-12-12

