js向upyun上传文件

先放实例:

实例github

<!DOCTYPE html>
<head>
<title>H5-put上传demo</title>
<script src="js/jquery-3.2.1.js"></script>
<script src="js/spark-md5.js"></script>
<script>
$(document).ready(function () {
    $("#fileupload").change(function () {
        bucket_name="bucket_name";              //服务名称
        opename="opename";                      //操作员账号
        opepass="opename";                      //操作员密码
        save_as="/rest.jpg";                    //保存路径
        acc_point="http://v0.api.upyun.com/";
        date=(new Date()).toUTCString();
        file=$(this).get(0).files[0];
        sign=SparkMD5.hash("PUT&/"+encodeURI(bucket_name+save_as)+"&"+date+"&"+file.size+"&"+SparkMD5.hash(opepass));
        infoHtml = "文件名称:" + file.name + "<br/>";
        infoHtml+= "文件大小:" + file.size + "<br/>";
        infoHtml+= "文件类型:" + file.type + "<br/>";
        $("#info").html(infoHtml);
    });
    $("#sendBtn").click(function () {
        var xhr = new XMLHttpRequest();
        xhr.upload.onprogress = function (event) {
            if (event.lengthComputable) {
                var percent = Math.round(event.loaded * 100 / event.total);
                $("#progress").show();
                $("#percent").text(percent + "%");
                $("#bar").width( percent+'%');
            };
        };
        xhr.onload=function(event){
          $("#progress").hide();
          if(xhr.status==200){
              alert("上传成功");
          }else{alert("上传失败,代码:"+JSON.parse(xhr.responseText).code);}
        };
        xhr.open('PUT', acc_point+encodeURI(bucket_name+save_as), true);
        xhr.setRequestHeader("Authorization","UpYun "+opename+":"+sign);
        xhr.setRequestHeader("X-Date",date);
        xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        xhr.send(file);
    });
});
</script>
</head>
<body style="padding-top:50px;">
<div style="width:440px;height:240px;padding:20px;border:1px solid #C0C0C0;margin:0 auto;border-radius:4px;">
    <input id="fileupload" type="file" name="file" multiple/><br/>
    <br/>
    <br/>
    <button id="sendBtn">上传</button>
    <br/>
    <br/>
    <div id="info"></div>
    <br/>
    <br/>
    <div id='progress' style="width:300px;height:16px;border:1px solid #ddd;padding-top:1px;border-radius:2px;display:none">
    <div id='bar' style="float:left;background-color:#4693EC; width:0%; height:14px; border-radius:2px;"></div>
    <div id='percent' style="float:left;width:0px;display:inline-block; top:0px;font-size:13px;"></div>
    </div>
    <br/>
    <br/>
</div>
</body>
</html>

官网

http://docs.upyun.com/

客服

http://support.upyun.com/kchat/18915?group=22540&from=mainsite#/chatbox

https://github.com/upyun/node-sdk

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,009评论 19 139
  • First things first... SMILE. No matter what you currently...
    正念教练张林宝阅读 354评论 0 0
  • 2018年4月23日,东明中学初二级部全体同学走进潍坊滨海实践基地。 无线电猎狐,一定很有趣吧 ^-^ 午餐,大家...
    张瑞彤妈妈阅读 2,520评论 0 0
  • 我忘了是谁说过,一个整天喜欢热闹的人一定是没有真正爱过的人。我不知道这是不是真的,但我却能深深体会到,当一个人真正...
    给你的承诺阅读 2,801评论 19 46
  • 今天寝室停电,我去上厕所,尽管厕所没有灯,但是我并没有害怕,或者胡思乱想一些不好的事情。但是每当寝室熄灯,半夜自己...
    tswfm阅读 165评论 0 0