如何使用wxwidgets下载二进制文件

时间:2015-06-11 08:03:29

标签: c++ wxwidgets

我找到了下载文件的代码段:

 <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
    <script src="js/pauseAnimation.js"></script>
    <style>
        /*ALL DIV WRAPPER;*/

        html {
            margin: 0 auto;
            padding: 0;
        }

        #horizontalSlider {
            position: relative;
            overflow: hidden;
            width: 400px;
        }

        #horizontalSlider ul {
            position: relative;
            height: 180px;
            list-style: none;
            overflow: hidden;
            padding: initial;
        left: 0;
            /*width: 1800px;*/
        }

        .left {
            float: left;
        }

        #horizontalSlider ul li {
           position: relative;
            display: block;
            float: left;
            /*width: 200px;*/
            /*border: 1px solid gold;*/
            opacity: 0.3;
            transition: opacity 1s;
            -webkit-transition: opacity 1s;
        }

        #horizontalSlider ul li:hover {
            opacity: 1.0;
            /*filter: alpha(opacity=1);*/  
            transition: opacity 1s;
            -webkit-transition: opacity 1s;
        }

        .all {
            margin: 0 auto;
            width: 550px;
        }

        .a {
            padding: initial;
            margin: initial;
            text-decoration: none;
            color: black;
        }
    </style>

    <script type="text/javascript">
    var totalWidth = 0; //width of all elements of li
    var numerOfChildlenElements = 0; // the number of children li
    var currentChild = 0; //first from left display element
    var left = 0; // distance from the start size of ul

        function moveRight() {
             if( currentChild == 3) {
            left = (-totalWidth + $("ul").children().eq((numerOfChildlenElements - 1)).width());
            currentChild = 0;
             }
             else {
            left -=  $("ul").children().eq(currentChild);
            ++currentChild;
             }

            $('#horizontalSlider ul').css('left', left);
                $('#horizontalSlider ul').animate({left: '-='+$("ul").children().eq(((currentChild == 0) ? (numerOfChildlenElements -1) : (currentChild - 1))).width() }, 1000, 'linear', function (){});
            }

            function moveLeft() {
            if(currentChild == 3 || currentChild == -1) {
            left = (- (2*totalWidth) + $("ul").children().eq((numerOfChildlenElements-1)).width() );
             currentChild = (numerOfChildlenElements - 2);
            }
            else {
            left += $("ul").children().eq(currentChild);
            --currentChild;
            }

            $('#horizontalSlider ul').css('left', left);
                    $('#horizontalSlider ul').animate({left: '+='+$("ul").children().eq((currentChild)).width() }, 1000, 'linear', function () {


                });
            }
        $(document).ready(function() {

            numerOfChildlenElements = $("ul").children().length;
            for(i = 0; i < numerOfChildlenElements; ++i)
                totalWidth += $("ul").children().eq(i).width();
        //three times to duplicate elements
        $('#horizontalSlider ul').css("width", (3*totalWidth));
        $('#horizontalSlider ul li').clone().appendTo('#horizontalSlider ul');
        $('#horizontalSlider ul li:lt('+ numerOfChildlenElements +')').clone().appendTo('#horizontalSlider ul');
        left = -totalWidth;
        $('#horizontalSlider ul').css('left', left);

        $("#left").click(function(){
            moveLeft();
        });

        $("#right").click(function(){
            moveRight();
        });
    });
    </script>
    </head>
    <body>

        <div class="all">
            <button id="left" class="left">LEFT</button>
            <div id="horizontalSlider" class="left">
            <ul>
                    <li>
                         <div><img src="http://th02.deviantart.net/fs71/150/f/2011/178/9/3/06272011___naruto_x_tayuya_by_dthegrimm-d3k6edx.jpg" alt="zdj1" /></div>
                         <div>1.Praesent at sapien</div>
                    </li>
                    <li>
                        <a href="http://www.onet.pl" target="_blank">
                        <div><img src="http://th01.deviantart.net/fs44/150/f/2009/068/6/3/new_naruto_7_class_by_unhai.jpg" alt="zdj2" /></div>
                        <div>2.Nullam ut varius.</div>
                        </a>
                    </li>
                    <li>
                        <div><img src="http://www.creetor.com/games/images/naruto-ninja-world-storm-2.png" alt="zdj3" /></div>
                        <div>3.P3t at vestibulum sapien,</div>
                    </li>
                    <li>
                        <div><img src="http://th06.deviantart.net/fs70/150/i/2012/352/0/6/naruto_uzumaki_vs_son_goku_by_pinkycute03-d5oej1r.jpg" alt="zdj4" /></div>
                        <div>4.Orange akcesoria still</div>
                    </li>
                </ul>
            </div>
            <button id="right" class="right">RIGHT</button>
        </div>    
    </body>
    </html>

但是,它只是记录文件的内容,仅用于文本文件。但我需要下载* .exe文件以便稍后执行。因此,我需要调整此代码以使用二进制数据,并将此数据保存到磁盘上的文件中。这里使用太多Streams来了解这里发生了什么。请帮忙。

1 个答案:

答案 0 :(得分:1)

我之前写过以下代码... 这可以在任何平台上下载二进制文件。

 /** START */
 // ex) ht tp://mysite.com/mypath.jpg
 wxString path   = wxT("/mypath.jpg");
 wxString server = wxT("mysite.com");

 wxHTTP http; 
 http.SetHeader(_T("Content-type"), contentType); 
 http.SetTimeout(10);

 // wxString imageFilePath = wxT("/tmp/image.jpg");
 wxFileOutputStream output(imageFilePath);
 wxDataOutputStream store(output);

 if (http.Connect(server, 80)) 
 {
  wxInputStream *stream;
  stream = http.GetInputStream(path);

  if (stream == NULL) 
  {
       output.Close();
  } 
  else 
  {
       unsigned char buffer[1024];
       int byteRead;

       // receive stream
       while (!stream->Eof()) 
       {
        stream->Read(buffer, sizeof(buffer));
        store.Write8(buffer, sizeof(buffer));
        byteRead = stream->LastRead();
        if (byteRead <= 0) 
        {
         break;
        }
       }

       output.Close();
  }
 } 
 else 
 {
  output.Close();
 }