簡體   English   中英

“遠程服務器返回錯誤:(501)參數或參數中的語法錯誤。”從Windows Azure上傳到FTP時

[英]“The remote server returned an error: (501) Syntax error in parameters or arguments.” when uploading TO FTP FROM Windows Azure

我需要不斷將生成的文件從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
        }
    }
}

如果將UsePassive設置為false,則需要確保命令通道的端口已打開(即,您需要定義端點和訪問規則)。 除非有充分的理由不使用被動,否則你最好不要使用被動。

埃里克

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM