2015-02-06 46 views
1

我为应用程序写了一个dll,它将通过ftp从应用程序上传一些信息到服务器。上传的大部分文件都能正常上传。CInternetSession :: WriteString写入文件并直接删除

有一个文件将完全上传,但上传之后直接从服务器上删除(这种情况不总是发生,有时该文件在上传后存在于服务器上)。该文件大约400 kb,其他所有文件都较小。

日期和类型是两个CStrings。数据包含文件内容并键入文件名的第一部分。

CInternetSession session(_T("whtsnXt_dll")); 
CFtpConnection* pServer = NULL; 
CInternetFile* pFile = NULL; 
LPCTSTR pszServerName = _T("servername"); 
CString fileName = type + L".txt"; 
int curPos = 0; 
CString postData = data; 

try 
{ 
    CString strServerName; 
    INTERNET_PORT nPort = 21; 

    pServer = session.GetFtpConnection(pszServerName, _T("username"), _T("password"), nPort, TRUE); 
    if (pServer->SetCurrentDirectory(L"goes") == 0) { 
     //  MessageBox::Show("De map bestaat niet", "whtsnXt error", MessageBoxButtons::OK, MessageBoxIcon::Error); 
    } 
    pFile = pServer->OpenFile((LPCTSTR)fileName, GENERIC_WRITE); 
    pFile->WriteString(postData); 
    pFile->Close(); 
    pServer->Close(); 

    delete pFile; 
    delete pServer; 
} 
catch (CInternetException* pEx) 
{ 
    //catch errors from WinInet 
    TCHAR pszError[64]; 
    pEx->GetErrorMessage(pszError, 64); 
      MessageBox::Show(gcnew String(pszError), "whtsnXt error", MessageBoxButtons::OK, MessageBoxIcon::Error); 
} 
session.Close(); 

有人知道一种方法来直接上传该文件,直接删除?

+0

测试时文件名是否有所不同? – thomiel 2015-02-07 10:32:29

+0

有不同的文件上传与所有不同的名称。但他们在一起总是有相同的名字(出错的文件是rooster0.txt)。该文件在上传过程中和上传后被覆盖(当文件存在时) – 2015-02-07 13:56:30

+0

您是否测试了另一个ftp服务器? – thomiel 2015-02-07 16:56:15

回答

2

尝试将文件上传较小的部分:

int i; 
for (i = 0; i < postData.Getlength(); i += 1024) 
{ 
    pFile->WriteString(postData.Mid(i, min(1024, postData.Getlength() - i)); 
} 

只是可以肯定:数据实际上是一个多字节或Unicode字符串,并持有不二进制数据? WriteString只会写入,直到找到'\ 0'字符。要上传二进制数据,请使用pFile->Write