我需要不断将生成的文件从Azure上传到客户端的FTP,但是当我运行下面的代码时,它会给我...
远程服务器返回错误:(501)参数或参数中的语法错误。
...也在Azure模拟器中它工作正常。
这是概念验证目的草案代码,所以我没有故意使用Worker角色,队列或blob等......
using System;
using System.IO;
using System.Net;
using System.Web;
namespace CloudWeb
{
/// <summary>
/// Summary description for Ftp
/// </summary>
public class Ftp : IHttpHandler
{
private const string FtpHost = "ftp://ftp.Host.com/App_Data/{0}";
private const string FtpUserName = "UserName";
private const string FtpPassword = "Password";
private const string WarningImageFile = "images/status_warning.png";
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
UploadFtp(context.Server.MapPath(WarningImageFile));
context.Response.Write("Hello World");
}
public bool IsReusable
{
get
{
return false;
}
}
private static void UploadFtp(string source)
{
// read local file
byte[] bFile = File.ReadAllBytes(source);
// set ftp values
var myFtp = (FtpWebRequest)WebRequest.Create(String.Format(FtpHost, Path.GetFileName(source)));
myFtp.Method = WebRequestMethods.Ftp.UploadFile;
myFtp.UsePassive = false;
myFtp.UseBinary = true;
myFtp.KeepAlive = true;
myFtp.Credentials = new NetworkCredential(FtpUserName, FtpPassword);
// upload file
using (Stream clsStream = myFtp.GetRequestStream())
{
clsStream.Write(bFile, 0, bFile.Length);
clsStream.Close();
//clsStream.Dispose();
}
// ReSharper disable RedundantAssignment
myFtp = null;
// ReSharper restore RedundantAssignment
}
}
}
答案 0 :(得分:4)
如果将UsePassive设置为false,则需要确保命令通道的端口已打开(即,您需要定义端点和访问规则)。除非有充分的理由不使用被动,否则你最好不要使用被动。
埃里克