tencentyun / cos-js-sdk-v5

腾讯云 COS JS SDK(XML API)
https://cloud.tencent.com/product/cos
MIT License
329 stars 566 forks source link

javascript直传cos时怎么对上传的文件进行重命名 #55

Closed CyberSage-hub closed 5 years ago

CyberSage-hub commented 5 years ago

使用过程中发现不能自动重命名,难道需要上传成功后再调用移动文件接口的方法实现重命名吗? cos.moveFile(successCallBack, errorCallBack, bucket, path, destPath, overWrite); https://cloud.tencent.com/document/product/436/8095#.E6.9B.B4.E6.96.B0.E6.96.87.E4.BB.B6.E5.B1.9E.E6.80.A7

CyberSage-hub commented 5 years ago

使用过程中发现不能自动重命名,难道需要上传成功后再调用移动文件接口的方法实现重命名吗? cos.moveFile(successCallBack, errorCallBack, bucket, path, destPath, overWrite); https://cloud.tencent.com/document/product/436/8095#.E6.9B.B4.E6.96.B0.E6.96.87.E4.BB.B6.E5.B1.9E.E6.80.A7

已解决,上传接口中Key值随机一下就行了

        // 随机产生字符串
        var randomstr = function (len){
            var str = "";
            var arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
            for(var i=0; i<len; i++){
                var pos = Math.round(Math.random() * (arr.length-1));
                str += arr[pos];
            }
            return str;
        }
        // 监听选文件
        document.getElementById('pic').onchange = function () {
            var file = this.files[0];
            if (!file) return;

            // 分片上传文件
            cos.sliceUploadFile({
                Bucket: Bucket,
                Region: Region,
                Key: randomstr(10)+file.name,
                Body: file,
                onHashProgress: function (progressData) {
                    console.log('校验中', JSON.stringify(progressData));
                },
                onProgress: function (progressData) {
                    console.log('上传中', JSON.stringify(progressData));
                },

            }, function (err, data) {
                console.log('上传结果完成',err, data);
                console.log('上传结果完成Location',data.Location);
            });

        };