vue前端上传图片预览

前端上传图片在浏览器上预览,不调接口,刷新后图片就会没有

<div class="btn">
   <button>上传背景图片<input id="fileToUpload" 
   style="position: absolute; top: 0; bottom: 0; left: 0;right: 0; opacity: 0;" 
   type="file" accept="image/jpg,image/png" name="fileToUpload" 
   @change="showImg($event)" multiple /></button>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue前端组件可以使用第三方库如`element-ui`的`el-upload`组件来实现上图片和视频的功能。 首先,需要在Vue项目中引入`element-ui`库: ```javascript import Vue from 'vue' import ElementUI from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' Vue.use(ElementUI) ``` 然后,在需要使用上组件的页面中,可以使用`el-upload`组件来实现上图片和视频的功能。以下是一个示例: ```html <template> <div> <el-upload class="upload-demo" action="/upload" :before-upload="beforeUpload" :on-success="handleSuccess" :on-error="handleError" :file-list="fileList" :accept="'image/*,video/*'" > <el-button size="small" type="primary">点击上</el-button> <div slot="tip" class="el-upload__tip">只能上图片或视频</div> </el-upload> </div> </template> <script> export default { data() { return { fileList: [] } }, methods: { beforeUpload(file) { const isImage = file.type.indexOf('image') !== -1 const isVideo = file.type.indexOf('video') !== -1 const isLt2M = file.size / 1024 / 1024 < 2 if (!isImage && !isVideo) { this.$message.error('请选择图片或视频文件') return false } if (!isLt2M) { this.$message.error('上文件大小不能超过 2MB') return false } }, handleSuccess(response, file, fileList) { console.log(response, file, fileList) this.fileList = fileList.slice(-1) }, handleError(error, file, fileList) { console.log(error, file, fileList) this.$message.error('上失败') } } } </script> ``` 其中,`el-upload`组件的`accept`属性可以用来限制上文件的类型,本示例中使用`image/*,video/*`表示只允许上图片或视频文件;`beforeUpload`方法用来在上前进行文件类型和大小的校验;`handleSuccess`和`handleError`方法分别用来处理上成功和失败的情况。 需要注意的是,上文件的具体实现需要在后端服务器中完成。在本示例中,`action`属性指定了上文件的后端接口地址为`/upload`,具体实现需要后端开发人员完成。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值