问题
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