src/qcloud_sdk.js
function sliceInit(opt) {
        var defer = $.Deferred();
        var file = opt.file;
        var that = this;

        var url = this.getCgiUrl(opt.path, opt.sign);

        var formData = new FormData();
        var uploadparts = opt.uploadparts;
        formData.append('uploadparts', JSON.stringify(uploadparts));
        formData.append('sha', opt.sha);
        formData.append('op', 'upload_slice_init');
        formData.append('filesize', file.size);
        formData.append('slice_size', opt.sliceSize);
        formData.append('biz_attr', opt.biz_attr);
        formData.append('insertOnly', opt.insertOnly);

        $.ajax({
            type: 'POST',
            dataType: "JSON",
            url: url,
            data: formData,
            success: function (res) {
                if (opt.globalTask.state === 'cancel') return;
                res = res || {};
                if (res.code == 0) {

                    if (res.data.access_url) {//如果秒传命中则直接返回
                        defer.resolve(res);
                        return;
                    }
                    var session = res.data.session;
                    var sliceSize = parseInt(res.data.slice_size);

                    var offset = res.data.offset || 0;

                    opt.session = session;
                    opt.slice_size = sliceSize;
                    opt.offset = offset;

                    sliceUpload.call(that, opt).done(function (r) {
                        defer.resolve(r);
                    }).fail(function (r) {
                        defer.reject(r);
                    });

                    // 保存正在上传的 session 文件分片 sha1，用于下一次续传优化判断是否不一样的文件
                    var sItem, sha1Samples = {};
                    for (var i = 1; i < opt.uploadparts.length; i *= 2) {
                        sItem = opt.uploadparts[i - 1];
                        sha1Samples[sItem.offset + '-' + sItem.datalen] = sItem.datasha;
                    }
                    sItem = opt.uploadparts[opt.uploadparts.length - 1];
                    sha1Samples[sItem.offset + '-' + sItem.datalen] = sItem.datasha;
                    rememberSha1(opt.session, sha1Samples, that.sha1CacheExpired);

                } else {
                    defer.reject(res);
                }
            },
            error: function () {
                defer.reject();
            },
            processData: false,
            contentType: false
        });

        return defer.promise();
    }
function sliceInit(opt) {
        var defer = $.Deferred();
        var file = opt.file;
        var that = this;

        var url = this.getCgiUrl(opt.path, opt.sign);

        var formData = new FormData();
        var uploadparts = opt.uploadparts;
        formData.append('uploadparts', JSON.stringify(uploadparts));
        formData.append('sha', opt.sha);
        formData.append('op', 'upload_slice_init');
        formData.append('filesize', file.size);
        formData.append('slice_size', opt.sliceSize);
        formData.append('biz_attr', opt.biz_attr);
        formData.append('insertOnly', opt.insertOnly);

        $.ajax({
            type: 'POST',
            dataType: "JSON",
            url: url,
            data: formData,
            success: function (res) {
                if (opt.globalTask.state === 'cancel') return;
                res = res || {};
                if (res.code == 0) {

                    if (res.data.access_url) {//如果秒传命中则直接返回
                        defer.resolve(res);
                        return;
                    }
                    var session = res.data.session;
                    var sliceSize = parseInt(res.data.slice_size);

                    var offset = res.data.offset || 0;

                    opt.session = session;
                    opt.slice_size = sliceSize;
                    opt.offset = offset;

                    sliceUpload.call(that, opt).done(function (r) {
                        defer.resolve(r);
                    }).fail(function (r) {
                        defer.reject(r);
                    });

                    // 保存正在上传的 session 文件分片 sha1，用于下一次续传优化判断是否不一样的文件
                    var sItem, sha1Samples = {};
                    for (var i = 1; i < opt.uploadparts.length; i *= 2) {
                        sItem = opt.uploadparts[i - 1];
                        sha1Samples[sItem.offset + '-' + sItem.datalen] = sItem.datasha;
                    }
                    sItem = opt.uploadparts[opt.uploadparts.length - 1];
                    sha1Samples[sItem.offset + '-' + sItem.datalen] = sItem.datasha;
                    rememberSha1(opt.session, sha1Samples, that.sha1CacheExpired);

                } else {
                    defer.reject(res);
                }
            },
            error: function () {
                defer.reject();
            },
            processData: false,
            contentType: false
        });

        return defer.promise();
    }
function sliceInit ( opt ) { var defer = $ . Deferred ( ) ; var file = opt . file ; var that = this ; var url = this . getCgiUrl ( opt . path , opt . sign ) ; var formData = new FormData ( ) ; var uploadparts = opt . uploadparts ; formData . append ( 'uploadparts' , JSON . stringify ( uploadparts ) ) ; formData . append ( 'sha' , opt . sha ) ; formData . append ( 'op' , 'upload_slice_init' ) ; formData . append ( 'filesize' , file . size ) ; formData . append ( 'slice_size' , opt . sliceSize ) ; formData . append ( 'biz_attr' , opt . biz_attr ) ; formData . append ( 'insertOnly' , opt . insertOnly ) ; $ . ajax ( { type : 'POST' , dataType : "JSON" , url : url , data : formData , success : function ( res ) { if ( opt . globalTask . state === 'cancel' ) return ; res = res || { } ; if ( res . code == 0 ) { if ( res . data . access_url ) { //如果秒传命中则直接返回
 defer . resolve ( res ) ; return ; } var session = res . data . session ; var sliceSize = parseInt ( res . data . slice_size ) ; var offset = res . data . offset || 0 ; opt . session = session ; opt . slice_size = sliceSize ; opt . offset = offset ; sliceUpload . call ( that , opt ) . done ( function ( r ) { defer . resolve ( r ) ; } ) . fail ( function ( r ) { defer . reject ( r ) ; } ) ; // 保存正在上传的 session 文件分片 sha1，用于下一次续传优化判断是否不一样的文件
 var sItem , sha1Samples = { } ; for ( var i = 1 ; i < opt . uploadparts . length ; i *= 2 ) { sItem = opt . uploadparts [ i - 1 ] ; sha1Samples [ sItem . offset + '-' + sItem . datalen ] = sItem . datasha ; } sItem = opt . uploadparts [ opt . uploadparts . length - 1 ] ; sha1Samples [ sItem . offset + '-' + sItem . datalen ] = sItem . datasha ; rememberSha1 ( opt . session , sha1Samples , that . sha1CacheExpired ) ; } else { defer . reject ( res ) ; } } , error : function ( ) { defer . reject ( ) ; } , processData : false , contentType : false } ) ; return defer . promise ( ) ; }
slice upload init
slice upload init