Hi,
I've been playing around with SFML's HTTP Client (downloading files, getting file sizes using HEAD etc), and I was wondering if there's anyway to get the amount of bytes that have currently been downloaded.
At first I just thought "well just stick the download in a new thread and keep checking the size of the file." However, I realised that you aren't writing to the file until everything has already downloaded... bit of an oversight for me haha.
Any ideas anyone?
Edit:
Download code:
int download(std::string host, std::string uri, std::string filename) {
sf::Http http;
http.setHost(host);
sf::Http::Request request;
request.setUri(uri);
sf::Http::Response response = http.sendRequest(request);
if (response.getStatus() == sf::Http::Response::Ok) {
std::ofstream file(filename, std::ios::out | std::ios::binary);
file << response.getBody();
file.close();
//std::cout << "Downloaded " << host << uri << " to " << filename << "!" << std::endl;
return jb::ReturnValues::DOWNLOAD_SUCCESSFUL;
}
else{
std::cout << "Error: " << response.getStatus() << std::endl;
return jb::ReturnValues::DOWNLOAD_FAILED;
}
}
Getting remote file size code:
int getRemoteFileSize(std::string host, std::string uri) {
sf::Http http;
http.setHost(host);
sf::Http::Request request;
request.setMethod(sf::Http::Request::Head);
request.setUri(uri);
sf::Http::Response response = http.sendRequest(request);
if (response.getStatus() == sf::Http::Response::Ok) {
return stoi(response.getField("Content-Length"));
}
else{
return jb::ReturnValues::GET_REMOTE_FILE_SIZE_FAILED;
}
}
(Mostly SFML's example code rn I know
)