首页 > javascript相关 > vue教程 > 正文

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

2018-10-22 21:15:42

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

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

OSS平台配置

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

关于跨域访问的配置

这里是我的效果图 (当只有点击上传按钮时才会上传到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>

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

  • 相关标签:vue教程
  • 本文发布HTML5中文学习网 ,转载请注明出处,感谢您!
  • 相关文章


  • 曝网友假装外国人写投诉信 ofo秒退押金并回函致歉
  • 苹果市值缩水逾2000亿美元 遭多家投行下调目标价
  • Asp.net Core与类库读取配置文件信息的方法_实用技巧
  • asp.net在Repeater嵌套的Repeater中使用复选框详解_实用技巧
  • 利用IIS调试ASP.NET网站程序的完整步骤_实用技巧
  • Asp.Net Core轻松学习系列之配置文件_实用技巧
  • ASP.NET 页生命周期概述(小结)_实用技巧
  • 详解ASP.NET Core WebApi 返回统一格式参数_实用技巧
  • 2018年网络流行语有哪些?2018年十大网络流行语盘点
  • 华为首席财务官孟晚舟被暂扣 深圳市政府要求加方立即放人!
  • 独孤九贱(4)_PHP视频教程

    江湖传言:PHP是世界上最好的编程语言。真的是这样吗?这个梗究竟是从哪来的?学会本课程,你就会明白了。 PHP中文网出品的PHP入门系统教学视频,完全从初学者的角度出发,绝不玩虚的,一切以实用、有用...

    独孤九贱(5)_ThinkPHP5视频教程

    ThinkPHP是国内最流行的中文PHP开发框架,也是您Web项目的最佳选择。《php.cn独孤九贱(5)-ThinkPHP5视频教程》课程以ThinkPHP5最新版本为例,从最基本的框架常识开始,将...

    独孤九贱(1)_HTML5视频教程

    《php.cn原创html5视频教程》课程特色:php中文网原创幽默段子系列课程,以恶搞,段子为主题风格的php视频教程!轻松的教学风格,简短的教学模式,让同学们在不知不觉中,学会了HTML知识。 ...

    ThinkPHP5实战之[教学管理系统]

    本套教程,以一个真实的学校教学管理系统为案例,手把手教会您如何在一张白纸上,从零开始,一步一步的用ThinkPHP5框架快速开发出一个商业项目。

    PHP入门视频教程之一周学会PHP

    所有计算机语言的学习都要从基础开始,《PHP入门视频教程之一周学会PHP》不仅是PHP的基础部分更主要的是PHP语言的核心技术,是学习PHP必须掌握的内容,任何PHP项目的实现都离不开这部分的内容,通...

    作者信息

    kevin

    永远在学习的路上!

    相关教程

  • javascript初级视频教程 javascript初级视频教程
  • jquery 基础视频教程 jquery 基础视频教程
  • javascript三级联动视频教程 javascript三级联动视频教程
  • 独孤九贱(3)_JavaScript视频教程 独孤九贱(3)_JavaScript视频教程
  • 独孤九贱(6)_jQuery视频教程 独孤九贱(6)_jQuery视频教程
  • 热门教程