溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

詳解node+vue如何實(shí)現(xiàn)文件上傳功能

發(fā)布時(shí)間:2020-07-21 09:35:15 來(lái)源:億速云 閱讀:247 作者:小豬 欄目:web開(kāi)發(fā)

小編這次要給大家分享的是詳解node+vue如何實(shí)現(xiàn)文件上傳功能,文章內(nèi)容豐富,感興趣的小伙伴可以來(lá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" >

   <!--此處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)稍后重試!');
  })
  },

看完這篇關(guān)于詳解node+vue如何實(shí)現(xiàn)文件上傳功能的文章,如果覺(jué)得文章內(nèi)容寫(xiě)得不錯(cuò)的話(huà),可以把它分享出去給更多人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI