小編這次要給大家分享的是詳解node+vue如何實(shí)現(xiàn)文件上傳功能,文章內(nèi)容豐富,感興趣的小伙伴可以來了解一下,希望大家閱讀完這篇文章之后能夠有所收獲。
成都創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比玉泉網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式玉泉網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋玉泉地區(qū)。費(fèi)用合理售后完善,十載實(shí)體公司更值得信賴。
*后端*
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" > <!--此處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)聽上傳時(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, //類型 userName:localStorage.getItem('username') //那個(gè)用戶上傳的 } }).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)稍后重試!'); }) },
看完這篇關(guān)于詳解node+vue如何實(shí)現(xiàn)文件上傳功能的文章,如果覺得文章內(nèi)容寫得不錯(cuò)的話,可以把它分享出去給更多人看到。
分享名稱:詳解node+vue如何實(shí)現(xiàn)文件上傳功能
鏈接分享:http://chinadenli.net/article20/jdssco.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計(jì)、品牌網(wǎng)站建設(shè)、動(dòng)態(tài)網(wǎng)站、全網(wǎng)營銷推廣、網(wǎng)站建設(shè)、營銷型網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
移動(dòng)網(wǎng)站建設(shè)知識(shí)