[C++] MFC FTP Uploader

Hi.

I've currently been scripting a C++ dialog-based FTP Uploader (using MFC), in VS2010, following [url]http://www.cpp-home.com/tutorials/256_1.htm[/url].

Everything worked out fine, corrected the bug on "#include "ifxinet.h"", should be afxinet..

However, there is a problem when it comes to uploading..

This is the part which refers to uploading, and which causes the error:

1
2
3
4
5
6
7
if(ftpConn->PutFile(m_maplocation, m_maplocation, FTP_TRANSFER_TYPE_BINARY, 10) == 0)
{
TRACE("Error uploading file: %dn", GetLastError());
MessageBox("Unable to upload file");
}
else
MessageBox("File successfully uploaded"); 


Okay, that's exactly how it was in the tutorial. Since m_maplocation doesn't exist, I thought it was probably m_filelocation, which was told to be added before (and which is the name of the file which is browsed). Compiles fine, like always, but still, "Unable to upload file".

What exactly might be the problem? Has anyone tested this code before and it's only a problem for this specific server?

I'm a beginner when it comes to programming, so please explain how to do the things you ask me to do (so that I can do them correctly and also learn).

If you need anything, please ask for it.

Thanks a lot,
Kelthar

PS: To explain the situation, in case there's a better solution, what I need is to upload a file to an FTP server, to a specific folder, and the password, server and username are already included in the source code, so that any user can use the executable and upload files without having access to "confidential" information. Since the server isn't mine, I can't create an account which can only upload to a specific folder and make it public while telling the users to use FileZilla or any other FTP client. If there's any better solution, please tell me..
There should be a number in that TRACE message. Find it and look it up: It should tell you the problem or lead you in the right direction.
I'm a newbie at this, so how can I make it show me what it traced? ;s
On line 2 of your example, you have a TRACE statement. When you uploaded it and had an error, that line was displayed: "Error uploading file: %dn" however, the '%dn' part was filled in with an error code. What was it?
When it uploaded it executed MessageBox("Unable to upload file"); not TRACE. I'll remove the box to see what happens.
Why don't you put the same message in the Message box? Or, find the TRACE file for your code?
As I said before, I'm kind of a newbie at programming, how can I find the TRACE file? :S Or put the same message in the message box?

Sorry for being such a pain in the ass.

Note: I need to go now, I'll do what you tell me to do tomorrow, thanks by the way.
Well, since you are using MS V2010, just put a break point on the TRACE line ...
Okay so I evolved a bit.

Now it does upload files, but the problem comes to the file names..

1
2
	m_filelocation = cfd.GetFileName();
	converter = cfd.GetPathName();


That is the part which stores the names.

if(ftpConn->PutFile(converter, m_filelocation, FTP_TRANSFER_TYPE_BINARY, 10) == 0)

And that is the part which sends the file.

The thing is, if the filename is too long (for some reason), m_filelocation WILL show file path!

It will upload file with for example C:\file\file123456789.txt instead of file123456789.txt

This is critical since this is for a program which will load files based on what you input. If you search for file123456789 it will say it loaded C:\file\file123456789.txt, but having the C:\file part will be quite annoying.

Thanks!

EDIT: Think it's if the name is longer than 20. Not sure.
Last edited on
The cfd.GetFileName() looks like it only returns the file name part (the part without any paths in it.) The cfd.GetPathName() looks like it gets the path part (or even the path part plus the file name part.)

You really need to know both. The FTP server puts you into a default directory, usually based upon your user information. If you want to copy the file into a different directory, you should be able to do a "cd" to the directory you want (as long as your account has permission.) You really should read up on the basics of FTP

 
if(ftpConn->PutFile(m_maplocation, m_maplocation, FTP_TRANSFER_TYPE_BINARY, 10) == 0)

equals to:
 
if(ftpConn->PutFile(m_maplocation, m_maplocation, FTP_TRANSFER_TYPE_BINARY, 10))


You did copy-paste without reading it, didn't you?
Well, the code means that if the function returning !true! so it'll send the failure message :/.. it's just a little mistake of the writer.
replace it with this:
 
if(!ftpConn->PutFile(m_maplocation, m_maplocation, FTP_TRANSFER_TYPE_BINARY, 10) )
Last edited on
@kooth It already navigates to the directory I need, that's the first thing it does after logging in. The problem in here is, for some reason, the way it'll use cfd.GetPathName instead of cfd.GetFileName in certain circumstances..

In the upload function, the first one, which is named converter, is a cfd.GetPathName so that it can load the file. The second one, named m_filelocation, is a cfd.GetFileName, so that it can deploy the file to the directory I had selected before. However, when the file name is bigger than 20 (also including the file type), it will deploy the file with the name of the path.

Everything works completely fine and it does exactly what I need, except for the name part.

I'll try and search for an optional way to retrieve the name without using cfd.GetFileName to see if it's because of that string or not really, so that I have a clue on what's exactly wrong.

@Fresh123 It does work correctly.. if(ftpConn->PutFile(m_maplocation, m_maplocation, FTP_TRANSFER_TYPE_BINARY, 10) == 0) means that if it FAILS, it will show that message. If you look it up at MSDN, PutFile returns 0 if it fails, therefore the code is correct.
Last edited on
Well that's not what I remember O_O...
Wow I guess I was using too much this syntax:
1
2
if(ftpConn->PutFile(m_maplocation, m_maplocation, FTP_TRANSFER_TYPE_BINARY, 10))


that I forgot this thing.. :S
Well If you problem is for the target name make sure that the name is after the path.. I mean:
Path+"/"+Name..
For example if your target Dic is public_html and the file name is "bla.txt" then the target path is:
public_html/bla.txt

You can do the editing with a string and than use String.c_str()...
Last edited on
The upload part is now working perfectly fine, it uploads fine.

However, certain files will upload the whole path name (they will upload to the directory I want but the name will be the path it was on the uploader's drive), and others will upload regularly.

Not sure why does this happen.. ;S
Topic archived. No new replies allowed.