指定されたパス形式は、ファイルをftpにアップロードする際のC#Windowsアプリケーションではサポートされていません

ラジム・カーン

ファイルをftpにアップロードしたいのですが、エラーが発生します。指定されたパスの形式はサポートされていません。私は以下の私のコードに親切に助けてくれます。エラーを与えるパス、パスをどのように保つ必要がありますか?

以下は私のパスですthis.path = @ " ftp:// ip address / Requests /";

   public bool UploadDocsToFTP(string SourceFilePath, string FileName)
    {

        string ServerUri = "";
        string FTPUserName = "";
        string FTPPassword = "";
        string ftpURI = "";

        try
        {
            try
            {
                ServerUri = ConfigurationManager.AppSettings["LinuxFileServerUri"];
                FTPUserName = ConfigurationManager.AppSettings["LinuxFtpUserName"];
                FTPPassword = ConfigurationManager.AppSettings["LinuxFtpPassword"];

                string[] splitfilename = SourceFilePath.Split('\\');
                //String businesRegId = splitfilename[2];
                //String userRegId = splitfilename[3];
                //String folderType = splitfilename[3];

                //ftpURI = "ftp://" + ServerUri + "//" + businesRegId + "//" + userRegId;
                //if (!string.IsNullOrEmpty(folderType))
                //    ftpURI += "//" + folderType;

                //ServerUri = "ftp://" + ServerUri + "//" + businesRegId + "//" + userRegId + "//";
                //if (!string.IsNullOrEmpty(folderType))
                //    ServerUri += folderType + "//";
                // SetMethodRequiresCWD();

                FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ip address/Requests" + FileName);
                request.Method = WebRequestMethods.Ftp.UploadFile;
                request.Credentials = new NetworkCredential(FTPUserName, FTPPassword);

                FileStream fs = File.OpenRead(SourceFilePath + FileName);
                byte[] buffer = new byte[fs.Length];
                fs.Read(buffer, 0, buffer.Length);
                fs.Close();

                Stream ftpstream = request.GetRequestStream();
                ftpstream.Write(buffer, 0, buffer.Length);
                ftpstream.Close();
            }
            catch (WebException e)
            {
                String status = ((FtpWebResponse)e.Response).StatusDescription;

                if (UploadDocsToFTP_DirNotExists(SourceFilePath, FileName))
                {
                    return true;
                }

                return false;
            }

        }
        catch (Exception ex)
        {
            ex.ToString();
            return false;
        }
        return true;
    }
DrewJordan

URL(パスが何であるか)にはスペースやその他の特定の文字を含めることができないため、エンコードする必要があります。

これにSystem.Net.WebUtility使用できます

// instead of this: 
// FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ip address/Requests" + FileName);

// do this: 
string path = System.Net.WebUtility.UrlEncode("ftp://ip address/Requests" + FileName);   
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(path);

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Related 関連記事

ホットタグ

アーカイブ