You can send a
Range
header:request.setRequestHeader("Range", "bytes=0-9999");
request.send(null);
Note that the server might ignore this header and you will get the usual response back. In most cases the response will be "206 Partial Content" however with exactly 10000 bytes of data.
Content-Range
response header indicates which part of the file you've got, e.g.request.getResponseHeader("Content-Range")
might give you bytes 0-9999/1234567
(here 1234567 is the total size of the file).
Obviously, you can also do
request.setRequestHeader("Range", "bytes=100000-119999");
to get data from the middle of the file.
The following code can be used to download the first 10k of a sites html as per the first part of the question...
Lets say I only need first 10kb of the page
function getURL(url, limit, callback) {
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if ( request.readyState == 4 ) {
if (request.responseText!=0) callback( request.responseText, request.status );
} else if (request.responseText.length >= limit) {
// If limit is exceeded
var result = request.responseText;
request.abort(); // Cancel request
callback( result, request.status );
}
};
request.overrideMimeType("text/html");
request.open( "GET", url, true );
request.send();
}
getURL('http://www.google.com.au', 100000, debug);
//getURL('http://paez.kodingen.com/testy.png', 100000, debug);
function debug(responseText, status) {
console.debug('length of responseText '+responseText.length);
console.debug('responseStatus : '+status);
console.debug('responseText :\n'+responseText);
}
Note
It should be noted that this wont get exactly the size you specify as their is no way to say how often the readystate will be called. Also, I force it to be text otherwise their may not be a responseText.
It should be noted that this wont get exactly the size you specify as their is no way to say how often the readystate will be called. Also, I force it to be text otherwise their may not be a responseText.
Here when readyState is not 4, which mean loading state; So here we can abort request when size is exceeded.