断点下载/断点续传

2018-12-12  本文已影响13人  四季变幻

推荐阅读(排版精美版):https://my.oschina.net/u/2400070/blog/2987864

知识点一:

获取服务器文件信息,不下载(https://chuanke.baidu.com/v1867921-207967-1271723.html,扩展阅读:https://chuanke.baidu.com/v1867921-207968-1271734.html)

http://www.runoob.com/ajax/ajax-examples.html

方式1,ajax方式(只支持同域):

http://www.runoob.com/jquery/ajax-ajax.html

http://wvoice.spriteapp.cn/voice/2015/0818/55d2248309b09.mp3

var head = document.head || document.getElementsByTagName('head')[0];

var script = document.createElement('script');

script.src = 'https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js';

head.appendChild(script);

$.ajax({url:'http://wvoice.spriteapp.cn/voice/2015/0818/55d2248309b09.mp3?_' + new Date().getTime(), type: 'HEAD', success:function(result, status, xhr) {

console.log('dd');

console.log(xhr.getAllResponseHeaders());

}});

方式2,fetch(React-native中使用)(https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API)

let url = 'http://wvoice.spriteapp.cn/voice/2015/0818/55d2248309b09.mp3';

    fetch(url, {

      method: 'HEAD'

    })

    // .then(response => response.json())

    .then((headers) => {

      console.log(headers);

    })

    .catch(error => {

      console.error(error);

    });

请求方式概览:

知识点二:

断点续传,关键参数:Range

示例:

Ajax分段获取示例:

$(document).ready(function(){

$("#b01").click(function(){

$.ajax({

headers:{

'Range':'bytes=0-2'

},

url:"/jquery/test1.txt",

async:false,

success: function(ret) {

console.log(ret);

}

});

});

});

上一篇 下一篇

猜你喜欢

热点阅读