By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,389 Members | 1,881 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,389 IT Pros & Developers. It's quick & easy.

Internet File Downloading - How do I do this?

P: n/a
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

Feb 23 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
* 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 23 '07 #2

P: n/a
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

"Steve" <st******************@gmail.comwrote in message
news:11*********************@j27g2000cwj.googlegro ups.com...
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

Feb 23 '07 #3

P: n/a
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

On Feb 22, 7:23 pm, "Ron AF Greve" <news moonlit xs4all nlwrote:
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
Feb 23 '07 #4

P: n/a
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" <st******************@gmail.comwrote in message
news:11*********************@k78g2000cwa.googlegro ups.com...
>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

On Feb 22, 7:23 pm, "Ron AF Greve" <news moonlit xs4all nlwrote:
>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

Feb 23 '07 #5

P: n/a
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

Feb 23 '07 #6

P: n/a
Ron AF Greve wrote:
Hi

Here is some ezample code include the wininet.h header.
Completely off-topic. Also, 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>
Feb 23 '07 #7

P: n/a
Steve wrote:
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>
Feb 23 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.