学无先后,达者为师

网站首页 编程语言 正文

express+element实现上传图片

作者:筱闯~ 更新时间: 2023-07-14 编程语言

首先定义后端我们需要配express静态资源文件

在app.js中配置

下载 npm i multiparty 插件

app.use('/upload',express.static("upload"))//静态文件

然后新建一个upload文件夹

express中定义接口

var multiparty = require("multiparty")//调用插件
//上传图片
router.post("/imgs", (req, res) => {
  let form = new multiparty.Form()
  form.uploadDir = "upload"
  form.parse(req, (err, arr, imge) => {
    let imgs = "http://localhost:3000/" + imge.file[0].path
    res.send({
      imgs,
      code: 200,
      msg: "成功"
    })
  })
})

前端利用element-ui组件库

<el-upload class="avatar-uploader" action="http://localhost:3000/imgs" :show-file-list="false" :on-success="handleAvatarSuccess">
   <img v-if="updfrom.imgs" :src="updfrom.imgs" class="avatar">
   <i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>
//上面修改咱们定义的接口


 //上传图片
handleAvatarSuccess(res, file) {
 this.imageUrl = URL.createObjectURL(file.raw);
 this.addfrom.imgs = res.imgs//展示照片
}

原文链接:https://blog.csdn.net/m0_64544033/article/details/130101642

  • 上一篇:没有了
  • 下一篇:没有了
栏目分类
最近更新