Internet File Downloading - How do I do this?

S

Steve

Hello,
I am a beginner/intermediate c++ coder. I have a program that I am
writing and would like to be able to distribute to some of my
friends. The problem with this is that the data files required by it
are rather large, and most of them have email accounts with filesize
limits. What I want to do is upload the files they'll need to my
webpage and then have the program itself check to see if the files are
present in the directories (I know how to do this part) and if they
aren't found, then connect to my homepage and download the files.
So basically, the code flow will go like this:
User runs program
Check for File A
File A Not Found
Prompt User -> Not Found, Would you like to Download?
if User says Yes

//THIS IS THE PART I NEED TO BE ABLE TO DO
DownloadFile(File A) from MyHomepage
//END PART I NEED TO KNOW

if user says no
exit program

I found some confusing crap about MFC and etc, but I really need
something in straight c/c++ that can do this same thing, if possible.
If win32 API is a must, or easier, then that would work too I suppose,
but I'm trying to find the most flexible solution I can. Thanks in
advance!
-Steve
 
A

Alf P. Steinbach

* Steve:
Hello,
I am a beginner/intermediate c++ coder. I have a program that I am
writing and would like to be able to distribute to some of my
friends. The problem with this is that the data files required by it
are rather large, and most of them have email accounts with filesize
limits. What I want to do is upload the files they'll need to my
webpage and then have the program itself check to see if the files are
present in the directories (I know how to do this part) and if they
aren't found, then connect to my homepage and download the files.
So basically, the code flow will go like this:
User runs program
Check for File A
File A Not Found
Prompt User -> Not Found, Would you like to Download?
if User says Yes

//THIS IS THE PART I NEED TO BE ABLE TO DO
DownloadFile(File A) from MyHomepage
//END PART I NEED TO KNOW

if user says no
exit program

I found some confusing crap about MFC and etc, but I really need
something in straight c/c++ that can do this same thing, if possible.
If win32 API is a must, or easier, then that would work too I suppose,
but I'm trying to find the most flexible solution I can. Thanks in
advance!

The C++ standard library doesn't support this directly. You could run a
second program to do the downloading, or use a library. Many such
libraries are available.
 
R

Ron AF Greve

Hi

Here is some ezample code include the wininet.h header. This is microsoft
specific is not going to work on any unix. All the Internet and Ftp
functions are microsoft specific. On unix either write your own transfer
protocol using sockets (it really isn't that difficult) or if you want to
use ftp either use system in combination with a .netrc file and a macro
(don't forget to set the permissions chmod 600 on the .netrc file or it will
not work) or link to a ftp library (though I don't know of any free one).

Cheers,

if( InternetAttemptConnect( 0 ) == ERROR_SUCCESS )
{
HINTERNET Session;

if( !( Session = InternetOpen( _T( "Pollux" ), INTERNET_OPEN_TYPE_DIRECT,
0, 0, 0 ) ) )
{
AfxMessageBox( _T( "Could setup internet session" ) );
Succeeded = false;
}
else
{
HINTERNET Connection;
if( !( Connection = InternetConnect( Session, m_Server,
INTERNET_DEFAULT_FTP_PORT, m_User,
m_Password, INTERNET_SERVICE_FTP, 0, 0 ) ) )
{
AfxMessageBox( _T( "Could not establish ftp session" ) );
Succeeded = false;
}
else
{
if( !FtpCreateDirectory( Connection, _T( "Cons" ) ) )
{
//AfxMessageBox( "Could not Create 'Cons' dir on server!" );
// Likely it already existed since we try this every time :)
}


if( !FtpCreateDirectory( Connection, _T( "Prog" ) ) )
{
//AfxMessageBox( "Could not Create 'Cons' dir on server!" );
// Likely it already existed since we try this every time :)
}

for( int Cnt = 0; Cnt < sizeof( Files ) /
sizeof( TCHAR* ); Cnt++ )
{
FtpPutFile( Connection, Files[ Cnt ], Files[ Cnt ],
INTERNET_FLAG_TRANSFER_BINARY, NULL );
}


InternetCloseHandle( Connection );

}

InternetCloseHandle( Session );
}
}
else
{
AfxMessageBox( CString( "Could not connect to " ) + m_Server );
Succeeded = false;
}

if( Succeeded )AfxMessageBox( "De webpages staan nu op de
server",MB_ICONINFORMATION|MB_OK );




}



Regards, Ron AF Greve

http://www.InformationSuperHighway.eu
 
S

Steve

I was looking over this, and it kindof looks like this code will
upload a file from the local computer to a server, but I'm trying to
download something from a server to my local computer. Or am I
reading it wrong? Thanks!
-Steve
 
R

Ron AF Greve

Hi Steve,

No, you are perfectly right :)

It was just a cut and paste of some code of mine.

My guess is that you just replace the put with a get. However do look up on
either msdn or in the microsoft help information. I think you also have to
add some libraries to the linker (somewhere in the properties linker input
tab from the top of my head). Libraries I think wininet, ws_32 or something
but all of that is on the helppages.

Regards, Ron AF Greve

http://www.InformationSuperHighway.eu

Steve said:
I was looking over this, and it kindof looks like this code will
upload a file from the local computer to a server, but I'm trying to
download something from a server to my local computer. Or am I
reading it wrong? Thanks!
-Steve

Hi

Here is some ezample code include the wininet.h header. This is microsoft
specific is not going to work on any unix. All the Internet and Ftp
functions are microsoft specific. On unix either write your own transfer
protocol using sockets (it really isn't that difficult) or if you want to
use ftp either use system in combination with a .netrc file and a macro
(don't forget to set the permissions chmod 600 on the .netrc file or it
will
not work) or link to a ftp library (though I don't know of any free one).

Cheers,

if( InternetAttemptConnect( 0 ) == ERROR_SUCCESS )
{
HINTERNET Session;

if( !( Session = InternetOpen( _T( "Pollux" ),
INTERNET_OPEN_TYPE_DIRECT,
0, 0, 0 ) ) )
{
AfxMessageBox( _T( "Could setup internet session" ) );
Succeeded = false;
}
else
{
HINTERNET Connection;
if( !( Connection = InternetConnect( Session, m_Server,
INTERNET_DEFAULT_FTP_PORT, m_User,
m_Password, INTERNET_SERVICE_FTP, 0, 0 ) ) )
{
AfxMessageBox( _T( "Could not establish ftp session" ) );
Succeeded = false;
}
else
{
if( !FtpCreateDirectory( Connection, _T( "Cons" ) ) )
{
//AfxMessageBox( "Could not Create 'Cons' dir on server!" );
// Likely it already existed since we try this every time :)
}

if( !FtpCreateDirectory( Connection, _T( "Prog" ) ) )
{
//AfxMessageBox( "Could not Create 'Cons' dir on server!" );
// Likely it already existed since we try this every time :)
}

for( int Cnt = 0; Cnt < sizeof( Files ) /
sizeof( TCHAR* ); Cnt++ )
{
FtpPutFile( Connection, Files[ Cnt ], Files[ Cnt ],
INTERNET_FLAG_TRANSFER_BINARY, NULL );
}

InternetCloseHandle( Connection );

}

InternetCloseHandle( Session );
}
}
else
{
AfxMessageBox( CString( "Could not connect to " ) + m_Server );
Succeeded = false;
}

if( Succeeded )AfxMessageBox( "De webpages staan nu op de
server",MB_ICONINFORMATION|MB_OK );

}

Regards, Ron AF Greve

http://www.InformationSuperHighway.eu
 
J

Jack

I think the easiest thing is to create a socket to your webserver and
download the file and there are tons and tons of examples on the web
for it. just look up socket.h
 
D

Default User

Steve said:
I was looking over this, and it kindof looks like this code will
upload a file from the local computer to a server, but I'm trying to
download something from a server to my local computer. Or am I
reading it wrong? Thanks!

You're off-topic. Go elsewhere. Please don't top-post. Your replies
belong following or interspersed with properly trimmed quotes. See the
majority of other posts in the newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top