简体   繁体   中英

Ftp File Upload

BOOL uploadFile(char *filename, char *destination_name, char *address, char *username, char *password)
{
    BOOL t = false;
    HINTERNET hint, hftp;
    hint = InternetOpen("FTP", INTERNET_OPEN_TYPE_PRECONFIG, 0, 0, INTERNET_FLAG_ASYNC);
    hftp = InternetConnect(hint, address, INTERNET_DEFAULT_FTP_PORT, username, password, INTERNET_SERVICE_FTP, 0 , 0);
    t = FtpPutFile(hftp, filename, destination_name, FTP_TRANSFER_TYPE_BINARY, 0);
    InternetCloseHandle(hftp);
    InternetCloseHandle(hint);
    return t;
}

This is my function to uploading file to server, it is good written ? And i am using in function

uploadFile(workFullPath,extractFilename(workFullPath),"address","login","password");

But my file does not appears on ftp.

You are not doing any error handling at all, so you have no way of knowing why the file does not get uploaded.

Whenever a WinInet functions fails, you can call GetLastError() to find out why it failed, per the WinInet documentation for each function.

If GetLastError() returns ERROR_INTERNET_EXTENDED_ERROR , use InternetGetLastResponseInfo() to get the server's error:

ERROR_INTERNET_EXTENDED_ERROR
12003
An extended error was returned from the server. This is typically a string or buffer containing a verbose error message. Call InternetGetLastResponseInfo to retrieve the error text.

See WinInet's Handling Errors documentation for an example of using InternetGetLastResponseInfo() .

Something else to pay attention to - you are calling InternetOpen() with the INTERNET_FLAG_ASYNC flag:

Makes only asynchronous requests on handles descended from the handle returned from this function.

But, you are not actually using WinInet asynchronously, so you should not be using that flag at all.

See WinInet's FTP Sessions documentation for more details about how to use the WinInet FTP functions.

Try something more like this instead:

BOOL uploadFile(char *filename, char *destination_name, char *address, char *username, char *password)
{
    BOOL res = FALSE;
    DWORD err;

    HINTERNET hint = InternetOpen("FTP", INTERNET_OPEN_TYPE_PRECONFIG, 0, 0, 0);
    if (hint == NULL)
    {
        err = GetLastError();
        // handle the error as needed...
        goto done;
    }

    HINTERNET hftp = InternetConnect(hint, address, INTERNET_DEFAULT_FTP_PORT, username, password, INTERNET_SERVICE_FTP, 0 , 0);
    if (hftp == NULL)
    {
        err = GetLastError();
        // handle the error as needed...
        goto cleanup;
    }

    res = FtpPutFile(hftp, filename, destination_name, FTP_TRANSFER_TYPE_BINARY, 0);
    if (!res)
    {
        err = GetLastError();
        // handle the error as needed...
    }

cleanup:
    if (hftp) InternetCloseHandle(hftp);
    if (hint) InternetCloseHandle(hint);

done:
    return res;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM