七牛云,js前端获取token上传思路demo

html代码

<!DOCTYPE html>
<html lang="zh-cn">

<head>
  <meta charset="UTF-8">
  <title>七牛云 - JavaScript SDK</title>
  <link href="images/favicon.ico" rel="shortcut icon">
  <link rel="stylesheet" href="./style.css">
</head>

<body>
  <div class="mainContainer">
    <div id="box">
      <button class="select-button">选择文件</button>
      <input class="file-input" type="file" id="select" />
    </div>
    <div style='overflow:hidden'><div id='totalBar' style='width:60%;margin-top:50px;'>
      <p class='speed'></p>
    </div>
  </div>
</body>
<style>
</style>
    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script src="https://unpkg.com/qiniu-js@2.5.5/dist/qiniu.min.js"></script>
<script src="./index.js"></script>
</html>

js代码

​
// $.ajax({url:"/api/uptoken",success: (res)=> initFileInput(res)})
  console.log(qiniu)
  // let token = res.uptoken;
  let token = '9OTcMoaFLersH7Gia307ekIlG1t5aFXpbk5VDk94:hMXHQ0brKY6werchb7qsmP_TrnM=:eyJzY29wZSI6InBoeWF1ZGl0IiwiZGVhZGxpbmUiOjE1ODg3MzU5ODh9'
  // z1华北 z2 华南
  let config = {
    useCdnDomain: true,
    region: qiniu.region.z1
  };
  let putExtra = {
    fname: "",
    params: {},
    mimeType: null
  };
  
  $("#select").change(function(){
    console.log(1111111111)
    let file = this.files[0];
    let key = file.name;
    let subscription;
    // 调用sdk上传接口获得相应的observable,控制上传和暂停
    let observable = qiniu.upload(file, key, token, putExtra, config);
    let observer ={
      next(result){                        //上传中(result参数带有total字段的 object,包含loaded、total、percent三个属性)
        let total = result.total;
        $(".speed").text("进度:" + total.percent + "% ");//查看进度[loaded:已上传大小(字节);total:本次上传总大小;percent:当前上传进度(0-100)]
    },
    error(err){                          //失败后
        alert(err.message);
    },
    complete(res){
      console.log(res)                   //成功后
        // ?imageView2/2/h/100:展示缩略图,不加显示原图
        // ?vframe/jpg/offset/0/w/480/h/360:用于获取视频截图的后缀,0:秒,w:宽,h:高
    }
    }
     // 取消上传
     // subscription.unsubscribe();
    observable.subscribe(observer)
  })


​

css代码

body {
  background-color: rgb(249, 249, 249);
}
.navbar {
  background-color: #fff;
}
.mainContainer {
  position: relative;
  top: 52px;
}
.mainContainer {
  width: 1170px;
  margin: 0 auto;
  padding: 15px 15px;
}
.mainContainer .row .tip li {
  list-style: none;
}
.mainContainer .nav-box ul li a {
  color: #777;
}
#box,
#box2 {
  margin-top: 20px;
  height: 46px;
}
.fragment-group {
  overflow: hidden;
  padding-left: 0;
}
.hide {
  visibility: hidden;
}
.fragment-group .fragment {
  float: left;
  width: 30%;
  padding-right: 10px;
  list-style: none;
  margin-top: 10px;
}
.file-input {
  display: inline-block;
  box-sizing: border-box;
  width: 130px;
  height: 46px;
  opacity: 0;
  cursor: pointer;
}

.mainContainer .select-button {
  position: absolute;
  background-color: #00b7ee;
  color: #fff;
  font-size: 18px;
  padding: 0 30px;
  line-height: 44px;
  font-weight: 100;
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.1);
}
.speed {
  margin-top: 15px;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值