elementUI组件upload上传后图片不显示问题

在开发中调用上传图片的接口

<el-upload
    :before-upload="beforePicUpload" //! 上传w判断
    :on-remove="handleSwiperRemove" //! 文件删除时的钩子
    :on-success="handleSwiperSuccess" // 文件上传成功时的钩子
    action="/api/upload/pic" //!图片上传的地址根据实际开发修改
    class="avatar-uploader"
    list-type="picture-card"
    multiple
    ref="SwiperlUpload"
    >
    <el-button
      size="small"
      type="primary"
     >点击上传</el-button>
  </el-upload>

上传后的数据返回

以上的方法会自动上传图片.上传成功后会调用成功的钩子函数,由于后端返回的路径是这样的
在这里插入图片描述
不显示的效果:
在这里插入图片描述

这时需要在成功上传的函数中做以下操作,在file.url下拼接:地址+图片路径

handleSwiperSuccess(response, file, fileList) {
	//! 拼接地址和图片路径 地址根据开发修改
      file.url = 'http://supw.work:8989' + response.data.url;
      console.log("轮播图打印file",file)
      this.form.urls.push({img:response.data.url});
   },

成功显示:
在这里插入图片描述

希望以上能对你有所帮助.

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以回答你的问题。为了在ElementUIUpload中使用七牛云储存图片,你需要进行以下步骤: 1. 首先,你需要在七牛云上创建一个存储空间,并获取到你的Access Key和Secret Key。 2. 然后,在你的Vue项目中安装七牛云的SDK,可以使用npm命令进行安装: ``` npm install qiniu-js --save ``` 3. 在你的Vue组件中,你需要引入ElementUIUpload组件,并添加一个自定义的上传方法。这个方法将会在文件上传时被调用: ```javascript <template> <el-upload class="upload-demo" action="/upload" :before-upload="beforeUpload" :on-success="handleSuccess" :on-error="handleError" :headers="headers" :data="data" :file-list="fileList" multiple> <el-button size="small" type="primary">点击上传</el-button> </el-upload> </template> <script> import { Upload } from 'element-ui'; import Qiniu from 'qiniu-js'; export default { components: { Upload }, data() { return { fileList: [], token: '', key: '', domain: 'http://xxx.xxx.xxx.xxx/', headers: { 'Authorization': '', 'x-qiniu-url': '' }, data: { token: '', key: '' } }; }, methods: { beforeUpload(file) { // 获取上传凭证 let that = this; return new Promise((resolve, reject) => { axios.get('YOUR_UPLOAD_URL') .then(response => { that.token = response.data.token; that.key = response.data.key; that.headers.Authorization = `UpToken ${that.token}`; that.headers['x-qiniu-url'] = that.domain; that.data.token = that.token; that.data.key = that.key; resolve(); }) .catch(error => { reject(error); }) }); }, handleSuccess(response, file, fileList) { // 上传成功回调 console.log(response); }, handleError(error, file, fileList) { // 上传失败回调 console.log(error); } } } </script> ``` 在这段代码中,我们引入了ElementUIUpload组件和七牛云的SDK,然后在data中定义了一些变量,包括上传凭证、上传文件的key、七牛的域名、请求头和请求参数等。在beforeUpload方法中,我们向你的服务器请求上传凭证,在请求成功后将认证信息和请求参数赋值给data和headers变量。在handleSuccess和handleError方法中,我们可以处理上传成功和上传失败的情况。 4. 最后,你需要在你的服务器端实现上传凭证的生成。可以使用七牛云的SDK,也可以使用其他语言的SDK,例如Java或Python。在服务器端生成上传凭证后,将凭证和上传文件的key返回给前端。 例如,在Node.js中,你可以使用qiniu-sdk: ```javascript const qiniu = require('qiniu'); const accessKey = 'YOUR_ACCESS_KEY'; const secretKey = 'YOUR_SECRET_KEY'; const bucket = 'YOUR_BUCKET_NAME'; const mac = new qiniu.auth.digest.Mac(accessKey, secretKey); const options = { scope: bucket, expires: 7200 }; const putPolicy = new qiniu.rs.PutPolicy(options); const uploadToken = putPolicy.uploadToken(mac); app.get('/uploadToken', (req, res) => { const key = req.query.filename; res.json({ token: uploadToken, key: key }); }); ``` 在这个例子中,我们使用qiniu-sdk生成上传凭证,并在/uploadToken路由中返回凭证和上传文件的key。 这样,你就可以在ElementUIUpload中使用七牛云存储图片了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值