小編這次要給大家分享的是詳解node+vue如何實現(xiàn)文件上傳功能,文章內(nèi)容豐富,感興趣的小伙伴可以來了解一下,希望大家閱讀完這篇文章之后能夠有所收獲。
成都創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設、高性價比玉泉網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式玉泉網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設找我們,業(yè)務覆蓋玉泉地區(qū)。費用合理售后完善,十載實體公司更值得信賴。
*后端*
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'); //設置 視頻文件存放位置 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:'服務器繁忙!' }) }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:'服務器繁忙!' }) } });
*前端*
<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({ //上傳文件存儲在后端 method: 'post', url: '/uploadVideo', data: fd, //監(jiān)聽上傳時間 //實現(xià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, //標題 videoType:that.videoType, //類型 userName:localStorage.getItem('username') //那個用戶上傳的 } }).then(res => { if (res.data.code === 1) { setTimeout(function () { //為什么延遲,為了使進度條走完 that.$message({ message: '上傳成功', type: 'success' }) }, 1000); } }).catch(err => { this.$message.error('服務器繁忙,請稍后重試!'); }); } }).catch(err => { this.$message.error('服務器繁忙,請稍后重試!'); }) },
看完這篇關于詳解node+vue如何實現(xiàn)文件上傳功能的文章,如果覺得文章內(nèi)容寫得不錯的話,可以把它分享出去給更多人看到。
分享名稱:詳解node+vue如何實現(xiàn)文件上傳功能
鏈接分享:http://m.2m8n56k.cn/article20/jdssco.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設計、品牌網(wǎng)站建設、動態(tài)網(wǎng)站、全網(wǎng)營銷推廣、網(wǎng)站建設、營銷型網(wǎng)站建設
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)