Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Anyway to get download progress using SFML?  (Read 3674 times)

0 Members and 1 Guest are viewing this topic.

the__cows

  • Newbie
  • *
  • Posts: 7
    • View Profile
Anyway to get download progress using SFML?
« on: February 05, 2016, 09:48:04 pm »
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 :P)
« Last Edit: February 05, 2016, 09:56:01 pm by the__cows »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Anyway to get download progress using SFML?
« Reply #1 on: February 06, 2016, 08:16:09 am »
With the HTTP class directly you can't do it. One way that might work is, if the server supports it, to do partial downloads (google it), which you also could use to make two threads downloading stuff. It's not trivial but should be doable.

Otherwise you'll have to fall back to TCP sockets.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

the__cows

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Anyway to get download progress using SFML?
« Reply #2 on: February 06, 2016, 10:55:16 am »
With the HTTP class directly you can't do it. One way that might work is, if the server supports it, to do partial downloads (google it), which you also could use to make two threads downloading stuff. It's not trivial but should be doable.

Otherwise you'll have to fall back to TCP sockets.

Ah, thanks, makes sense. Honestly, I'd rather do more complicated programming with SFML then use a different library like libcurl & cpp-netlib; tried for days to get them working and nada.

Thanks :)