zoukankan      html  css  js  c++  java
  • 记一下vue项目中资源上传七牛云

    首先使用npm安装

    npm install qiniu-js

    引入

    import * as qiniu from 'qiniu-js'
    let subscription=‘’
    直接上代码
    Preview(event){
     let that=this
     let file = event.target.files[0];
     let observable
     if (file.size > 1024 * 1024 * 200) {
    that.sizeTopic=true
    setTimeout(function(){
    that.sizeTopic=false
    },1000)
                     return false;
    }
     let name=event.target.files[0].name
     this.resources_name=name
     this.sendSuccess=true
    this.$http.get(process.env.API_ROOT+'api/sqiniu/uptoken?name='+name)
    .then((res) =>{
    let token = res.data.data.uptoken; //上传验证信息,前端通过接口请求后端获得 
                       //七牛API
                            let config = {
                               useCdnDomain: true,
                               disableStatisticsReport: false,
                               retryCount: 5,
                               region: qiniu.region.z2
                            };
                            let putExtra = {
                             fname: "",
                             params: {},
                             mimeType: null
                            };                      
                             let key = file.name;
                                 // 添加上传dom面板
                            let next = (response) =>{
                                let total = response.total; //上传进度提示
                                that.percent=parseInt(total.percent)                        
                                }
                            let complete= (res) =>{
                                that.resources_key=res.key,
                                that.resources_hash=res.hash,
    that.resources_size=res.fsize,
    that.resources_type=res.type
    that.sendTopic=true
    setTimeout(function(){
    that.sendTopic=false
    },1000)
                            }
                         //七牛云上传对象
                            let subObject={
                                next: next,
                               complete: complete,
                               error: error
                            }
    // let subscription;
    let error= (err) =>{
    that.failTopic=true
    setTimeout(function(){
    that.failTopic=false
    },1000)
    }
                         // 调用sdk上传接口获得相应的observable,控制上传和暂停
                         observable = qiniu.upload(file, key, token, putExtra, config);
                         this.subscription=observable.subscribe(subObject)
    })
    .catch(function (error) {
    console.log(error);
    });
    },
     
    this.subscription.unsubscribe()//取消上传方法
  • 相关阅读:
    光纤收发器的连接及收发器指示灯故障诊断
    Keepalive介绍及工作原理
    RabbitMQ消息队列集群配置-1
    python2 centos7 安装mysql-python库
    etcd服务的安装与配置 yum安装新版本
    ETCD集群安装配置及简单应用 老版本
    查看mysql 最大连接数,连接线程数
    TCP连接状态详解及TIME_WAIT过多的解决方法
    Linux 进程打开最大文件连接数Too many open files
    redis 连接数 修改
  • 原文地址:https://www.cnblogs.com/xk-one/p/10071702.html
Copyright © 2011-2022 走看看