How to download/read only the first 80KB of a file?

人盡茶涼 提交于 2019-12-01 07:28:48

问题


I am making a Greasemonkey script for a website that has many flash files. I'd like to make a hash of the flash, the problem is that the flash files are up to 10 megabytes.

This is slow; I'd like to be able to only grab the first 80KB to hash. The end result would be an easy way to blacklist certain flash files containing unwanted content. How does my script grab only the first 80 KB (or so) of a file?


回答1:


Send the range header in your AJAX request.

For example:

$.ajax ( {
    url:        'http://TARGET_SERVER.COM/TARGET_PATH/TARGET_FILE.FLV',
    headers:    { Range: "bytes=0-80000" },
    success:    function (Resp) {
                    console.log(Resp);
                }
} );

(For files that are on the same-domain as the target page.)



For cross-domain files use GM_xmlhttpRequest():

GM_xmlhttpRequest ( {
    method:     "GET",
    url:        'http://TARGET_SERVER.COM/TARGET_PATH/TARGET_FILE.FLV',
    headers:    { Range: "bytes=0-80000" },
    onload:     function (Resp) {
                    console.log(Resp.responseText);
                }
} );


来源:https://stackoverflow.com/questions/11060244/how-to-download-read-only-the-first-80kb-of-a-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!