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()//取消上传方法
  • 相关阅读:
    在子线程中更新ProgressBar为null
    有关ContentProvider及相关一系列的简单用法(持续添加)
    Android内容提供者使用及创建
    Android中关于时间的操作
    Cell的一些坑: UITableViewCell宽度,在iphone5的时候是320,在iphone6的时候为啥也是320?
    处理数据源(根据条目字数多少 ,动态显示一行里有多少个条目,类似天猫搜索历史)
    iOS开发之如何跳到系统设置里的各种设置界面
    Block作为property属性实现页面之间传值(代替Delegate代理与协议结合的方法)
    xcode7的那些坑-“Your binary is not optimized for iPhone 5” (ITMS-90096) when submitting
    PresentViewController切换界面(一些系统自带的页面切换动画)
  • 原文地址:https://www.cnblogs.com/xk-one/p/10071702.html
Copyright © 2011-2022 走看看