Retrieve file from internet

Hi,

A newby question ;-) :

I use C++ builder and I want to download a single file (comma de-limited data) from internet and save it in a file.

Can you give me a code sample how to do this?

Thanks a lot!

Comments

  • Use the TIdHTTP component from Indy.
    You can try: [code]IdHTTP1->Get("http://yourdomain.com/file.csv");[/code]

    Good luck!
  • I've had the same problem so I tried the following solution.
    The application consits in downloading a number of zipped files (here 80) and place them in a specified directory.
    The internet address is assumed to be "http://yourdomain.com/Archives/"
    The directory will be : "C:zip_dir"
    The file names are composed of the letter 'A' + a number in 3 digits + ".zip" (from: "A001.zip" to: "A080.zip")

    ***To recreate this application, place 2 TMemos, 1 TNMHTTP, and 1 TButtons on the form.
    The Check box is used to stop the downloading. When you want to stop the downloading (files too long for example), you have to check the box, and wait the end of the current downloading.

    ***Insert the following code into Button1's OnClick event:

    #include // <--- DO NOT forget this !
    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
    AnsiString Dir=""; //Directory where the files will be downloaded
    AnsiString FN_dld=""; //File name to download
    AnsiString url=""; //internet address of the file to be downloaded
    bool pause=false;
    Dir.sprintf("%s",AnsiString("C:\zip_dir\"));
    if (!DirectoryExists(Dir))
    {
    if (!CreateDir(Dir))
    {
    Memo1->Lines->Add("Cannot create Directory : "+Dir); // Comment in a memo
    Application->MessageBox("Cannot create Directory", "File Error", IDOK);
    pause=true;
    }
    }
    int i=1;
    while(i<=80 && !pause)
    {
    // "C:zip_dirA001.zip" to "C:zip_dirA080.zip"
    FN_dld.sprintf("%s",Dir); //the name of the file is composed of the letter 'A' + a number (i) in 3 digits + ".zip"
    FN_dld.cat_sprintf("%s%03d%s", AnsiString("A"), i,AnsiString(".zip"));
    // "http://yourdomain.com/Archives/A001.zip"
    url.sprintf("%s", AnsiString("http://yourdomain.com/Archives/"));
    url.cat_sprintf("%s%03d%s", AnsiString("A"), i, AnsiString(".zip"));
    // (T1) Here, can we have the length of the file + estimated downloading time ?
    NMHTTP1->InputFileMode=true; //Needed to download into a file
    NMHTTP1->Body=FN_dld;
    Memo1->Text="Downloading : "+url);
    NMHTTP1->Get(url); //Download the file
    // (T2) How to unzip here the file dowloaded ? (one file in each zip)
    if (CheckBox1->State==cbChecked) //CheckBox for pause = stop dowload at end of current one
    {
    pause=true;
    Memo1->Lines->Add("Pause at : "+AnsiString(i));
    }
    i++;
    }//end-while-i&&pause
    }

    ***Insert the following code into NMHTTP1's OnAuthenticationNeeded event:

    void __fastcall TForm1::NMHTTP1AuthenticationNeeded(TObject *Sender)
    {
    AnsiString AnID, APass;
    InputQuery("Authentication required", "Enter a user ID", AnID);
    InputQuery("Authentication required", "Enter a password", APass);
    NMHTTP1->HeaderInfo->UserId = AnID;
    NMHTTP1->HeaderInfo->Password = APass;
    ShowMessage("Authentication information in place, please retry the previous command");
    }

    ***Insert the following code into NMHTTP1's OnFailure event:

    void __fastcall TForm1::NMHTTP1Failure(CmdType Cmd)
    {
    Memo2->Lines->Add("The downloading of : "+NMHTTP1->Body+" has failed");
    }

    ***Insert the following code into NMHTTP1's OnRedirect event:

    void __fastcall TForm1::NMHTTP1Redirect(bool &Handled)
    {
    if (MessageDlg("This site is redirecting you to another site. Allow redirect?", mtConfirmation, TMsgDlgButtons() << mbYes << mbNo, 0) == mrNo)
    Handled = TRUE;
    }

    ***Insert the following code into NMHTTP1's OnSuccess event:

    void __fastcall TForm1::NMHTTP1Success(CmdType Cmd)
    {
    Memo2->Lines->Add("OK : "+NMHTTP1->Body);
    }
    //---------------------------------------------------------------------------

    [b]Please I need help in the following tasks :[/b][u][/u]
    T1- Getting file width and showing download progression (%)
    T2- Unzip files using BCB (witch free unzip software to use ?)

    Thanks a lot!

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories