• 记一下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()//取消上传方法
  • 相关阅读:
    HTML直接引用vue.min.js,bootstrap-vue.min.js,axios.min.js等开发一个页面(2)
    HTML直接引用vue.min.js,bootstrap-vue.min.js,axios.min.js等开发一个页面
    [Vue+Element UI]不知道总页码数时如何实现翻页
    [Vue] 报错: Uncaught (in promise)
    [Vue + Element UI] 单选框
    [Lombok] Lombok的使用和常用注解使用示例
    Eclipse的Web项目开发:Maven插件jetty服务器的关闭
    [Python] 电脑同时安装python2和python3, 如何实现切换使用
    [Yaml] YAML 入门教程
    k8s ha的安装
  • 原文地址:https://www.cnblogs.com/xk-one/p/10071702.html
Copyright © 2020-2023  润新知