【发布时间】:2011-11-14 12:32:35
【问题描述】:
我正在开发一个很小的应用程序,它只是将文件上传到 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 中给出了两个错误,
Error 1 error C2275:
'System::Net::WebRequestMethods::Ftp': 非法使用这种类型作为表达式 C:\Users\Me\documents\visual studio 2010\Projects\test1\test1\test1.cpp 70Error 2 error C2228: left of
'.UploadFile'must have class/struct/union C:\Users\Me\documents\visual studio 2010\Projects\test1\test1\test1.cpp 70
我不确定这里出了什么问题。
【问题讨论】:
-
除其他外,您的缩进。
-
是的,很抱歉,感谢您编辑它。这不是纯粹的c++,但在我的大学里它通常是c++,所以.... :(有什么帮助吗?
-
不,它根本不是 C++。它是一种相关但不同的语言,称为 C++/CLI。此外,这个问题更多的是关于 C++/CLI 语法,而不是关于将任何内容上传到 FTP 站点。请在未来创建精简的测试用例。
-
先生,我只是一个初学者,并且有这么大的东西可以使用,但我以后会小心的
-
将问题分解成小块是在生活的任何部分开展业务的好方法。而且你不是生活的初学者。 :)(“先生”不是必需的..有点奇怪,tbh!)