curl-library
Big problem: uploaded sFtp file's size is 0KB!
Date: Sat, 01 Nov 2008 18:08:58 +0800
As the code below shows, it succesfully created a file on specified sFtp server dierectory, however, the file's size was ZERO! No matter what the local file's size was!
Why? Any thing wrong?
Any help is grateful!
===============CODE==================
static size_t read_callback(void * pBuffer, size_t size, size_t nmemb, void * hFile)
{
DWORD dwNumberOfBytesRead = 0;
BOOL bResult = ReadFile( (HANDLE) hFile, pBuffer, size * nmemb, &dwNumberOfBytesRead, NULL);
return dwNumberOfBytesRead;
}
bool sFTPUpload(CString bsFileFullPath, CString bsFtpWorkPath, CString bsFileRltPath, CString bsUserPassword)
{
USES_CONVERSION;
FILE *hFile;
char * lpszCurlErrorBuffer[CURL_ERROR_SIZE];
CURLcode nCurlResult = CURL_LAST;
struct stat file_info;
DWORD dwLastError;
/*open the uploading file*/
int hd ;
hd = open(bsFileFullPath, O_RDONLY) ;
fstat(hd, &file_info);
close(hd) ;
hFile = fopen(bsFileFullPath, "r");
if (hFile == INVALID_HANDLE_VALUE)
{
dwLastError = GetLastError();
printf("open file failed!\n%s\nDiscribe:%s",bsFileFullPath, dwLastError);
curl_global_cleanup();
return false;
}
else printf("open file OK!\n%s\n",bsFileFullPath);
/*begin init*/
curl_global_init(CURL_GLOBAL_ALL);
CURL * hCurl = curl_easy_init();
if (!hCurl)
{
curl_global_cleanup();
printf("CURL-Library easy_init failed \n");
return false;
}
printf("CURL-Library init OK!\n");
/*begin easy_handle*/
{
struct curl_slist * headerlist = NULL;
CString sFTPPath = bsFtpWorkPath + bsFileRltPath;
printf("setopt remote_url:%s\n",sFTPPath);
/* enable error buffer */
curl_easy_setopt(hCurl, CURLOPT_ERRORBUFFER, lpszCurlErrorBuffer) ;
curl_easy_setopt(hCurl, CURLOPT_VERBOSE, 1);
/* enable uploading */
curl_easy_setopt(hCurl, CURLOPT_UPLOAD, TRUE) ;
curl_easy_setopt(hCurl,CURLOPT_TRANSFERTEXT, TRUE) ;
/* specify target */
curl_easy_setopt(hCurl, CURLOPT_URL, sFTPPath);
/* read function */
curl_easy_setopt(hCurl, CURLOPT_READFUNCTION, read_callback);
/* now specify which file to upload */
curl_easy_setopt(hCurl, CURLOPT_READDATA, hFile);
curl_easy_setopt(hCurl, CURLOPT_USERPWD, bsUserPassword);
curl_easy_setopt(hCurl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)file_info.st_size);
/* Now do perform! */
nCurlResult = curl_easy_perform(hCurl);
/* clean up the FTP commands list */
curl_slist_free_all (headerlist);
/* always cleanup */
curl_easy_cleanup(hCurl);
}
fclose(hFile); /* close the local file */
curl_global_cleanup();
if (nCurlResult == CURLE_OK)
{
printf("upload succeed!\n");
return true;
}
return false;
}
int main()
{
CString LocalFilefullpath,ServerWorkPath,sFtpFileRltpath,UserPwd;
LocalFilefullpath = "C:\\Documents and Settings\\user\\desktop\\synctest\\test.txt";
ServerWorkPath = "sftp://192.168.1.166/home/user/";
sFtpFileRltpath = "synctest/test.txt";
UserPwd = "user:pwd";
sFTPUpload(LocalFilefullpath, ServerWorkPath,sFtpFileRltpath,UserPwd);
return 0;
}
-------------------------------------------------------------------
新浪空间——与朋友开心分享网络新生活!(http://space.sina.com.cn/ )
Received on 2008-11-01