代码是在MSDN上的,稍做改变
public static void Do()
{
// uploadUrl="ftp://(IP)/testftp/test.txt"
string uploadUrl = Config.GetFtpServerUri();
// fileName ="c:\test.txt"
string fileName = Config.GetCsvFilePath();
// Create a Uri instance with the specified URI string.
// If the URI is not correctly formed, the Uri constructor
// will throw an exception.
ManualResetEvent waitObject; Uri target = new Uri(uploadUrl);
FtpState state = new FtpState();
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(target);
request.Method = WebRequestMethods.Ftp.UploadFile;
// The request is anonymous by default; the credential does not have to be specified.
// The example specifies the credential only to
// control how actions are logged on the server.
string userName = Config.GetFtpUser();
string userPwd = Config.GetFtpUserPassword(); request.Credentials = new NetworkCredential(userName, userPwd);
// request.
// set
// request.EnableSsl = true; // Store the request in the object that we pass into the
// asynchronous operations.
state.Request = request;
state.FileName = fileName; // Get the event to wait on.
waitObject = state.OperationComplete; // Asynchronously get the stream for the file contents.
request.BeginGetRequestStream(
new AsyncCallback(EndGetStreamCallback),
state
); // Block the current thread until all operations are complete.
waitObject.WaitOne(); // The operations either completed or threw an exception.
if (state.OperationException != null)
{
throw state.OperationException;
}
else
{
Console.WriteLine("The operation completed - {0}", state.StatusDescription);
}
} private static void EndGetStreamCallback(IAsyncResult ar)
{
FtpState state = (FtpState)ar.AsyncState; Stream requestStream = null;
// End the asynchronous call to get the request stream.
try
{
requestStream = state.Request.EndGetRequestStream(ar);
// Copy the file contents to the request stream.
const int bufferLength = 2048;
byte[] buffer = new byte[bufferLength];
int count = 0;
int readBytes = 0;
FileStream stream = File.OpenRead(state.FileName);
do
{
readBytes = stream.Read(buffer, 0, bufferLength);
requestStream.Write(buffer, 0, readBytes);
count += readBytes;
}
while (readBytes != 0);
Console.WriteLine("Writing {0} bytes to the stream.", count);
// IMPORTANT: Close the request stream before sending the request.
requestStream.Close();
// Asynchronously get the response to the upload request.
state.Request.BeginGetResponse(
new AsyncCallback(EndGetResponseCallback),
state
);
}
// Return exceptions to the main application thread.
catch (Exception e)
{
Console.WriteLine("Could not get the request stream.");
state.OperationException = e;
state.OperationComplete.Set();
return;
} }在执行到requestStream = state.Request.EndGetRequestStream(ar);就总是报550错误,
ftp是在linux上用vsftpd搭建的,
用useradd -g ftp testftp创建的用户,这是为什么,怎么解决,不胜感激!
public static void Do()
{
// uploadUrl="ftp://(IP)/testftp/test.txt"
string uploadUrl = Config.GetFtpServerUri();
// fileName ="c:\test.txt"
string fileName = Config.GetCsvFilePath();
// Create a Uri instance with the specified URI string.
// If the URI is not correctly formed, the Uri constructor
// will throw an exception.
ManualResetEvent waitObject; Uri target = new Uri(uploadUrl);
FtpState state = new FtpState();
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(target);
request.Method = WebRequestMethods.Ftp.UploadFile;
// The request is anonymous by default; the credential does not have to be specified.
// The example specifies the credential only to
// control how actions are logged on the server.
string userName = Config.GetFtpUser();
string userPwd = Config.GetFtpUserPassword(); request.Credentials = new NetworkCredential(userName, userPwd);
// request.
// set
// request.EnableSsl = true; // Store the request in the object that we pass into the
// asynchronous operations.
state.Request = request;
state.FileName = fileName; // Get the event to wait on.
waitObject = state.OperationComplete; // Asynchronously get the stream for the file contents.
request.BeginGetRequestStream(
new AsyncCallback(EndGetStreamCallback),
state
); // Block the current thread until all operations are complete.
waitObject.WaitOne(); // The operations either completed or threw an exception.
if (state.OperationException != null)
{
throw state.OperationException;
}
else
{
Console.WriteLine("The operation completed - {0}", state.StatusDescription);
}
} private static void EndGetStreamCallback(IAsyncResult ar)
{
FtpState state = (FtpState)ar.AsyncState; Stream requestStream = null;
// End the asynchronous call to get the request stream.
try
{
requestStream = state.Request.EndGetRequestStream(ar);
// Copy the file contents to the request stream.
const int bufferLength = 2048;
byte[] buffer = new byte[bufferLength];
int count = 0;
int readBytes = 0;
FileStream stream = File.OpenRead(state.FileName);
do
{
readBytes = stream.Read(buffer, 0, bufferLength);
requestStream.Write(buffer, 0, readBytes);
count += readBytes;
}
while (readBytes != 0);
Console.WriteLine("Writing {0} bytes to the stream.", count);
// IMPORTANT: Close the request stream before sending the request.
requestStream.Close();
// Asynchronously get the response to the upload request.
state.Request.BeginGetResponse(
new AsyncCallback(EndGetResponseCallback),
state
);
}
// Return exceptions to the main application thread.
catch (Exception e)
{
Console.WriteLine("Could not get the request stream.");
state.OperationException = e;
state.OperationComplete.Set();
return;
} }在执行到requestStream = state.Request.EndGetRequestStream(ar);就总是报550错误,
ftp是在linux上用vsftpd搭建的,
用useradd -g ftp testftp创建的用户,这是为什么,怎么解决,不胜感激!
解决方案 »
- 用.net的B/S模式,人员关系图像化展示他们之间的关系
- 我想学用C#做一个windows服务,谁能给一个简单明了的可执行代码例子,谢谢
- 请问:C#中,如何在导出的WORD页脚中插入页码(第几页/总页数)及打印日期?急切知道答案,万分感谢!
- DetailsView和SqlDataSource的问题
- 就这么多分了.大家帮忙.用cf1.0怎么调用像Process那样的东西
- 请问我要建个全局变量,让整个项目都可以调用,请问我应该写在哪里.
- C# DataSet数据清除!在线等
- C#调用存错过程的参数问题。
- 我该选择哪个公司啊?请大家帮我分析一下,谢谢了
- 如果通过C#APP向asp.net提供数据接口
- 关于event的困惑
- Response.Redirect问题请教?可不可把url变成参数传递?急!!!
不知道是不是"ftp://(IP)/testftp/test.txt"这个有问题,
实在不行,我打算用socket来实现了
===========================================
static void Upload(string fileName, string uploadUrl)
{
Stream requestStream = null;
FileStream fileStream = null;
FtpWebResponse uploadResponse = null;
try
{
FtpWebRequest uploadRequest =
(FtpWebRequest)WebRequest.Create(uploadUrl);
uploadRequest.Method = WebRequestMethods.Ftp.UploadFile;
// UploadFile is not supported through an Http proxy
// so we disable the proxy for this request.
uploadRequest.Proxy = null;
requestStream = uploadRequest.GetRequestStream();
fileStream = File.Open(fileName, FileMode.Open);
byte[] buffer = new byte[1024];
int bytesRead;
while (true)
{
bytesRead = fileStream.Read(buffer, 0, buffer.Length);
if (bytesRead == 0)
break;
requestStream.Write(buffer, 0, bytesRead);
}
// The request stream must be closed before getting
// the response.
requestStream.Close();
uploadResponse =
(FtpWebResponse)uploadRequest.GetResponse();
Console.WriteLine("Upload complete.");
}
catch (UriFormatException ex)
{
Console.WriteLine(ex.Message);
}
catch (IOException ex)
{
Console.WriteLine(ex.Message);
}
catch (WebException ex)
{
Console.WriteLine(ex.Message);
}
finally
{
if (uploadResponse != null)
uploadResponse.Close();
if (fileStream != null)
fileStream.Close();
if (requestStream != null)
requestStream.Close();
}
}
应该是
"ftp://(IP)/test.txt"