恢复从某个位置下载

发布于 2025-01-22 18:25:36 字数 1514 浏览 0 评论 0原文

暂停后我需要下载该文件。但是我不知道如何使用 https://github.com/yhirose/yhirose/cpp-httplib 。但是问题是,当下载恢复时,服务器开始首先向我发送文件。问题:如何告诉服务器我已经下载的件的大小,以便服务器仅发送必要的部分? 我的代码:

   std::string body;
   httplib::Client cli( url, port );
   cli.set_follow_location( true );
   
   int file_size = is_part_of_file ? GetFileSize( result_file_name.c_str() ) : 0; // it is downloaded part of the file
   int last_percent;
   auto res = cli.Get(
      file_path.c_str(), httplib::Headers(),
      [ & ]( const httplib::Response& response )
      {
         ( void )response;
         return *is_download;
      },
      [ & ]( const char* data, size_t data_length )
      {
         body.append( data, data_length );
         return *is_download;
      },
      [ & ]( uint64_t len, uint64_t total )
      {
         int percent = ( int )( len * 100 / total );
         if( last_percent != percent )
         {
            *p_percent = ( ( float )percent / 100 );
         }
         last_percent = percent;
         return *is_download;
      } );

   if( res )
   {
      std::ofstream out( result_file_name, std::ios::binary | std::ios::app );
      out << body;
      out.close();
   }
   else
   {
      if( is_part_of_file )
      {
         std::ofstream out( result_file_name, std::ios::binary | std::ios::app );
         out << body;
         out.close();
      }
      return false;
   }

I need to download the file after a pause. But I do not know how to implement it correctly using https://github.com/yhirose/cpp-httplib . But the problem is that when the download resumes, the server starts sending me the file first. Question: how do I tell the server the size of the piece that I have already downloaded, so that the server sends me only the necessary part?
My code:

   std::string body;
   httplib::Client cli( url, port );
   cli.set_follow_location( true );
   
   int file_size = is_part_of_file ? GetFileSize( result_file_name.c_str() ) : 0; // it is downloaded part of the file
   int last_percent;
   auto res = cli.Get(
      file_path.c_str(), httplib::Headers(),
      [ & ]( const httplib::Response& response )
      {
         ( void )response;
         return *is_download;
      },
      [ & ]( const char* data, size_t data_length )
      {
         body.append( data, data_length );
         return *is_download;
      },
      [ & ]( uint64_t len, uint64_t total )
      {
         int percent = ( int )( len * 100 / total );
         if( last_percent != percent )
         {
            *p_percent = ( ( float )percent / 100 );
         }
         last_percent = percent;
         return *is_download;
      } );

   if( res )
   {
      std::ofstream out( result_file_name, std::ios::binary | std::ios::app );
      out << body;
      out.close();
   }
   else
   {
      if( is_part_of_file )
      {
         std::ofstream out( result_file_name, std::ios::binary | std::ios::app );
         out << body;
         out.close();
      }
      return false;
   }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文