2011-09-10 68 views
-7

我正在上传一个文件上传到FTP服务器的小小应用程序。我已经检查了我的代码,但我很难找到问题。下面是代码,上传到FTP服务器

#include "stdafx.h" 

using namespace System; 

#include "iostream" 
#include <conio.h> 

using namespace System; 

void UploadFiles(String ^_FileName, String ^_UploadPath, String ^_FTPUser, String ^_FTPPass); 

int main() 
{ 
    // Upload file using FTP 
    UploadFiles("c:\\test.html", "ftp://playbabe.tk/public_html/test.html", "xxxxxx", "xxxxxx"); 
    return 0; 
} 

void UploadFiles(System::String ^_FileName, System::String ^_UploadPath, System::String ^_FTPUser, System::String ^_FTPPass) 
{ 
    System::IO::FileInfo ^_FileInfo = gcnew System::IO::FileInfo(_FileName); 

    // Create FtpWebRequest object from the Uri provided 
    System::Net::FtpWebRequest ^_FtpWebRequest = safe_cast<System::Net::FtpWebRequest^>(System::Net::FtpWebRequest::Create(gcnew Uri(_UploadPath))); 

    // Provide the WebPermission Credintials 
    _FtpWebRequest->Credentials = gcnew System::Net::NetworkCredential(_FTPUser, _FTPPass); 

    // By default KeepAlive is true, where the control connection is not closed 
    // after a command is executed. 
    _FtpWebRequest->KeepAlive = false; 

    // set timeout for 20 seconds 
    _FtpWebRequest->Timeout = 20000; 

    // Specify the command to be executed. 

    _FtpWebRequest->Method =System::Net::WebRequestMethods::Ftp.UploadFile; 

    // Specify the data transfer type. 
    _FtpWebRequest->UseBinary = true; 

    // Notify the server about the size of the uploaded file 
    _FtpWebRequest->ContentLength = _FileInfo->Length; 

    // The buffer size is set to 2kb 
    int buffLength = 2048; 
    array<System::Byte> ^buff = gcnew array<System::Byte>(buffLength); 

    // Opens a file stream (System.IO.FileStream) to read the file to be uploaded 
    System::IO::FileStream ^_FileStream = _FileInfo->OpenRead(); 

    try 
    { 
     // Stream to which the file to be upload is written 
     System::IO::Stream ^_Stream = _FtpWebRequest->GetRequestStream(); 

     // Read from the file stream 2kb at a time 
     int contentLen = _FileStream->Read(buff, 0, buffLength); 

     // Till Stream content ends 
     while (contentLen != 0) 
     { 
      // Write Content from the file stream to the FTP Upload Stream 
      _Stream->Write(buff, 0, contentLen); 
      contentLen = _FileStream->Read(buff, 0, buffLength); 
     } 

     // Close the file stream and the Request Stream 
     _Stream->Close(); 
     delete _Stream; 
     _FileStream->Close(); 
     delete _FileStream; 
    } 
    catch (Exception ^ex) 
    { 
     //MessageBox::Show(ex->Message, "Upload Error", MessageBoxButtons::OK, MessageBoxIcon::Error); 
     std::cout<<"error"; 
    } 

    getch(); 
} 

它给出两个错误在Visual C++ 2010 Express中,

错误1个错误C2275:'System::Net::WebRequestMethods::Ftp':非法使用这种类型作为表达式的C:\用户\我\ document \ visual studio 2010 \ Projects \ test1 \ test1 \ test1.cpp 70

Error 2 error C2228:left of '.UploadFile' must have class/struct/union C:\ Users \ Me \ documents \ visual studio 2010 \ Projects \ test1 \ test1 \ test1.cpp 70

我不知道这里发生了什么问题。

+0

其中,您的缩进。 –

+1

这不是C++代码。 –

+0

对不起,并感谢编辑它。这不是纯粹的C++,但在我的大学,它通常是C++所以...... :(任何帮助吗?) –

回答

0

我不知道C++/CLI,但下面的语句肯定是错误的:

_FtpWebRequest->Method = System::Net::WebRequestMethods::Ftp.UploadFile; 

System::Net::WebRequestMethods::Ftp是一种类型,并在错误信息表明(你可以给我们的行号!)你试图用它作为表达。

你想获得一个指向静态成员函数的指针吗?

您是否因此表达了以下内容?

_FtpWebRequest->Method = System::Net::WebRequestMethods::Ftp::UploadFile; 
+0

工作。非常感谢。问题解决了。非常感谢。 –

+0

我如何将它标记为已解决? –

+0

@Mohammad:点击打勾。 –