vue上传图片到oss的方法示例(图片带有删除功能)


Posted in Javascript onSeptember 27, 2018

最近Vue项目中,要将用户上传的图片全部上传到oss上,

OSS配置项请访问:https://help.aliyun.com/document_detail/64095.html?spm=a2c4g.11186623.6.773.kcD20n

OSS平台配置

在平台的概览里面看看自己的基础设置里面的读写权限是否改为了公共读,我这边只有配置公共读才上传并且回显图片成功,其他情况还请朋友告知,谢谢

关于跨域访问的配置

vue上传图片到oss的方法示例(图片带有删除功能)

这里是我的效果图 (当只有点击上传按钮时才会上传到OSS)

vue上传图片到oss的方法示例(图片带有删除功能)

预览图片

<template>
  <div class="vue-uploader">
    <div class="file-list">
      <section v-for="(file, index) of files" class="file-item draggable-item" :key="file.name">
        <img :src="file.src" alt="" ondragstart="return false;">
        <span class="file-remove" @click="remove(index)">+</span>
      </section>
      <section v-if="status == 'ready'" class="file-item">
        <div @click="add" class="add"></div>
      </section>
    </div>
    <section v-if="files.length != 0" class="upload-func">
      <div class="progress-bar">
        <section v-if="uploading" :width="(percent * 100) + '%'">{{(percent * 100) + '%'}}</section>
      </div>
      <div class="operation-box">
        <button v-if="status == 'ready'" @click="submit">上传</button>
        <button v-if="status == 'finished'" @click="finished">完成</button>
      </div>
    </section>
    <input type="file" @change="fileChanged" ref="file" multiple="multiple" accept="image/jpg,image/jpeg,image/png,image/bmp">
  </div>
</template>
<script>
  export default {
    data() {
      return {
        status: 'ready',
        files: [],
        point: {},
        uploading: false,
        percent: 0
      }
    },
    methods: {
      add() {
        this.$refs.file.click()
      },
      submit() {
        console.log(this.files)
        // if (this.files.length === 0) {
        //   console.warn('no file!');
        //   return
        // }
        var that=this
       // 这里是OSS
        const client = new OSS.Wrapper({
          region: 'oss-cn-hangzhou', 
          accessKeyId: 'your access key',
          accessKeySecret: 'your access secret',
          bucket: 'your bucket name'
        });
        const fNum = this.files;
        for(var i=0;i<fNum.length;i++){
          var f=fNum[i].file
          console.log(f)
          const Name=f.name
          console.log(Name)
          const suffix = Name.substr(Name.indexOf("."));
          const obj=this.timestamp();
          const storeAs = 'header/'+obj+suffix // 路径+时间戳+后缀名
          console.log(storeAs)
          client.multipartUpload(storeAs, f).then(function (result){
            console.log(result.res.requestUrls)
          })
        }
      },
      // 时间戳
      timestamp:function(){ 
        const time = new Date(); 
        const y = time.getFullYear(); 
        const m = time.getMonth()+1; 
        const d = time.getDate(); 
        const h = time.getHours(); 
        const mm = time.getMinutes(); 
        const s = time.getSeconds(); 
        const ms = time.getMilliseconds() 
        return ""+y+this.Add0(m)+this.Add0(d)+this.Add0(h)+this.Add0(mm)+this.Add0(s)+this.Add0(ms); 
      },
      Add0:function(m){ 
        return m<10?'0'+m : m; 
      } ,

      finished() {
        this.files = []
        this.status = 'ready'
        
      },
      remove(index) {
        this.files.splice(index, 1)
      },
      fileChanged() {
        const list = this.$refs.file.files
        for (let i = 0; i < list.length; i++) {
          if (!this.isContain(list[i])) {
            const item = {
              name: list[i].name,
              size: list[i].size,
              file: list[i]
            }
            this.html5Reader(list[i], item)
            this.files.push(item)
          }
        }
        this.$refs.file.value = ''
      },
      // 将图片文件转成BASE64格式
      html5Reader(file, item){
        const reader = new FileReader()
        reader.onload = (e) => {
          this.$set(item, 'src', e.target.result)
        }
        reader.readAsDataURL(file)
      },
      isContain(file) {
       return this.files.find((item) => item.name === file.name && item.size === file.size)
      },
    }
  }
</script>
<style>
.vue-uploader {
  border: 1px solid #e5e5e5;
}
.vue-uploader .file-list {
  padding: 10px 0px;
}
.vue-uploader .file-list:after {
  content: '';
  display: block;
  clear: both;
  visibility: hidden;
  line-height: 0;
  height: 0;
  font-size: 0;
}
.vue-uploader .file-list .file-item {
  float: left;
  margin-left: 10px;
  
  position: relative;
  width: 150px;
  text-align: center;
}
.vue-uploader .file-list .file-item img{
  width: 150px;
  height: 150px;
  border: 1px solid #ececec;
}
.vue-uploader .file-list .file-item .file-remove {
  position: absolute;
  right: 4px;
  display: none;
  top: 4px;
  width: 20px;
  height: 20px;
  font-size:20px;
  text-align: center;
  color: white;
  cursor: pointer;
  line-height: 20px;
  border-radius: 100%;
  transform: rotate(45deg);
  background: rgba(0, 0, 0, 0.5);
}
.vue-uploader .file-list .file-item:hover .file-remove {
  display: inline;
}
.vue-uploader .file-list .file-item .file-name {
  margin: 0;
  height: 40px;
  word-break: break-all;
  font-size: 14px;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
.vue-uploader .add {
  width: 150px;
  height: 150px;
  float: left;
  text-align: center;
  line-height: 150px;
  font-size: 30px;
  cursor: pointer;
  background: url(../assets/upImg.png) no-repeat; // 这里使用的是我本地图片
  background-size: 100% 100%;
}
.vue-uploader .upload-func {
  display: flex;
  padding: 10px;
  margin: 0px;
  background: #f8f8f8;
  border-top: 1px solid #ececec;
}
.vue-uploader .upload-func .progress-bar {
  flex-grow: 1;
}
.vue-uploader .upload-func .progress-bar section {
  margin-top: 5px;
  background: #00b4aa;
  border-radius: 3px;
  text-align: center; 
  color: #fff;
  font-size: 12px;
  transition: all .5s ease;
}
.vue-uploader .upload-func .operation-box {
  flex-grow: 0;
  padding-left: 10px;
}
.vue-uploader .upload-func .operation-box button {
  padding: 4px 12px;
  color: #fff;
  background: #007ACC;
  border: none;
  border-radius: 2px;
  cursor: pointer;
}
.vue-uploader > input[type="file"] {
  display: none;
}
</style>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
jQuery Easyui学习之datagrid 动态添加、移除editor
Jan 27 Javascript
javascript基于原型链的继承及call和apply函数用法分析
Dec 15 Javascript
canvas红包照片实例分享
Feb 28 Javascript
AngularJS通过ng-Img-Crop实现头像截取的示例
Aug 17 Javascript
微信小程序页面间值传递的两种方法
Nov 26 Javascript
微信小程序图表插件wx-charts用法实例详解
May 20 Javascript
vue App.vue中的公共组件改变值触发其他组件或.vue页面监听
May 31 Javascript
JS Array.from()将伪数组转换成数组的方法示例
Mar 23 Javascript
微信小程序实现拼图小游戏
Oct 22 Javascript
JavaScript实现鼠标移入随机变换颜色
Nov 24 Javascript
微信小程序实现首页弹出广告
Dec 03 Javascript
Vue 修改网站图标的方法
Dec 31 Vue.js
浅谈vue项目4rs vue-router上线后history模式遇到的坑
Sep 27 #Javascript
详解关于vue2.0工程发布上线操作步骤
Sep 27 #Javascript
解决betterScroll在vue中存在图片时,出现拉不动的问题
Sep 27 #Javascript
Vue中的v-for指令不起效果的解决方法
Sep 27 #Javascript
在vue中使用v-bind:class的选项卡方法
Sep 27 #Javascript
解决Vue中引入swiper,在数据渲染的时候,发生不滑动的问题
Sep 27 #Javascript
详解如何在vue项目中使用lodop打印插件
Sep 27 #Javascript
You might like
PHP 二维数组根据某个字段排序的具体实现
2014/06/03 PHP
PHP观察者模式示例【Laravel框架中有用到】
2018/06/15 PHP
JavaScript 实现模态对话框 源代码大全
2009/05/02 Javascript
解析js原生方法创建表格效率测试
2013/07/08 Javascript
jquery实现带复选框的表格行选中删除时高亮显示
2013/08/01 Javascript
JavaScript实现图片DIV竖向滑动的方法
2015/04/25 Javascript
js图片轮播特效代码分享
2015/09/07 Javascript
js和jq使用submit方法无法提交表单的快速解决方法
2016/05/17 Javascript
seaJs使用心得之exports与module.exports的区别实例分析
2017/10/13 Javascript
详细分析jsonp的原理和实现方式
2017/11/20 Javascript
微信小程序开发之改变data中数组或对象的某一属性值
2018/07/05 Javascript
react 国际化的实现代码示例
2018/09/14 Javascript
JavaScript键盘事件常见用法实例分析
2019/01/03 Javascript
js实现图片放大并跟随鼠标移动特效
2019/01/18 Javascript
Vue开发之封装上传文件组件与用法示例
2019/04/25 Javascript
详解vue-cli@2.x项目迁移日志
2019/06/06 Javascript
jQuery实现图片切换效果
2020/10/19 jQuery
Flask SQLAlchemy一对一,一对多的使用方法实践
2013/02/10 Python
详尽讲述用Python的Django框架测试驱动开发的教程
2015/04/22 Python
详解Python 数据库 (sqlite3)应用
2016/12/07 Python
ansible作为python模块库使用的方法实例
2017/01/17 Python
利用django如何解析用户上传的excel文件
2017/07/24 Python
python模块之sys模块和序列化模块(实例讲解)
2017/09/13 Python
cmd运行python文件时对结果进行保存的方法
2018/05/16 Python
Django之提交表单与前后端交互的方法
2019/07/19 Python
修改 CentOS 6.x 上默认Python的方法
2019/09/06 Python
pytorch实现mnist分类的示例讲解
2020/01/10 Python
如何在VSCode下使用Jupyter的教程详解
2020/07/13 Python
详解anaconda安装步骤
2020/11/23 Python
python 第三方库paramiko的常用方式
2021/02/20 Python
Feelunique澳大利亚:欧洲的化妆品零售电商
2019/12/18 全球购物
财务情况说明书范文
2014/05/06 职场文书
2015年售后服务工作总结
2015/04/25 职场文书
学校禁毒宣传活动总结
2015/05/08 职场文书
node.js使用express-fileupload中间件实现文件上传
2021/07/16 Javascript
JavaScript阻止事件冒泡的方法
2021/12/06 Javascript