locked
File download in C++ metro style app

    Question

  • I am trying to download a file from a server in a c++ metro style app by the below code:

    DownloadOperation^ DownLoad;

    String^ DownloadFileName = L"http://msdn.microsoft.com/en-us/library/windows/apps/hh770532.aspx";
     Uri^ source = ref new Uri(DownloadFileName);
     String^ Destination = "trial.aspx";

     Windows::Storage::StorageFolder^ TempFolder = ApplicationData::Current->LocalFolder;
     String^ str = TempFolder->Path;
     task<StorageFile^>(TempFolder->CreateFileAsync( "Test.txt", CreationCollisionOption::ReplaceExisting)).then([this](StorageFile^ file)
        {
      m_StoreFile = file;
     });

     BackgroundDownloader^ DownLoader = ref new BackgroundDownloader();
     DownLoad = DownLoader->CreateDownload(source, m_StoreFile);
     DownLoad->StartAsync(); 

    The download just starts. The file is created in the specified location but download does not happen. What else should I need to do to make this work? I also want to display the progress of the dwnload and also pause and restart option. I looked into DownLoadProgrss and the like but there is no samples in c++ . There seems to be more code samples in c#. Please tell me how I can set up a progress delegate. Any leads to some sampe code would be great help!!


    Friday, May 25, 2012 6:11 PM

Answers

All replies

  • Your code snipped worked just fine for me. If I renamed the test.txt with the downloaded content to a .html file, it displayed in IE. Are you saying there is no content in your test.txt file?

    Thanks!


    David Lamb

    • Marked as answer by its_me_here Saturday, May 26, 2012 3:37 AM
    • Unmarked as answer by its_me_here Saturday, May 26, 2012 3:37 AM
    Saturday, May 26, 2012 12:32 AM
    Moderator
  • thank you David.

    I tried this with an exe.May be the exe file is big and it takes time to download.

    I would like to know how I can display the download progress!!!

    Saturday, May 26, 2012 3:41 AM
  • To see an example of ohow to get download progress, please see the Background Transfer sample (http://code.msdn.microsoft.com/windowsapps/Background-Transfer-Sample-d7833f61).  In the project, go to Scenario1.xaml.cpp and look for HandleDownloadAsync().  This shows how to create an IAsyncOperationWithProgress and attach an AsyncOperationProgressHandler function.

    Sincerely,

    Dan Ruder [MSFT]

    Monday, June 04, 2012 6:41 PM
    Moderator