550 file unavailable error with FtpWebRequest class file upload. Works fine in FileZilla

前端 未结 4 597
借酒劲吻你
借酒劲吻你 2021-01-13 08:26

I\'m searched and found other questions on this but none have solved my issues. I\'m trying to upload a file via FTP using sample MSDN code. I get the The remote server retu

4条回答
  •  生来不讨喜
    2021-01-13 09:09

    It's difficult to tell without detailed information about the error, but it seems most likely that somewhere in your path there's a directory missing. One thing to try if you use any non-standard characters in your FTP file path is to use HttpUtility.UrlPathEncode on it prior to adding it to the Url.

    To get detailed troubleshooting information, I add the following inside the configuration element of my application's .config file:

    
      
        
          
            
          
        
        
          
            
          
        
      
      
        
      
      
        
        
      
      
    
    

    Also, just for reference, here's the code I use for uploads:

        private static FtpWebRequest CreateFtpWebRequest(string ftpUrl, string userName, string password, bool useSsl, bool allowInvalidCertificate, bool useActiveFtp)
        {
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpUrl);
            request.Credentials = new NetworkCredential(userName, password);
    
            if (useSsl)
            {
                request.EnableSsl = true;
    
                if (allowInvalidCertificate)
                {
                    ServicePointManager.ServerCertificateValidationCallback = ServicePointManager_ServerCertificateValidationCallback;
                }
                else
                {
                    ServicePointManager.ServerCertificateValidationCallback = null;
                }
            }
    
            request.UsePassive = !useActiveFtp;
    
            return request;
        }
    
        private static void UploadFileToServer(string ftpUrl, string userName, string password, bool useSsl, bool allowInvalidCertificate, bool useActiveFtp, string filePath)
        {
            FtpWebRequest request = CreateFtpWebRequest(ftpUrl, userName, password, useSsl, allowInvalidCertificate, useActiveFtp);
    
            request.Method = WebRequestMethods.Ftp.UploadFile;
    
            long bytesReceived = 0;
            long bytesSent = 0;
    
            using (Stream requestStream = request.GetRequestStream())
            using (FileStream uploadFileStream = File.OpenRead(filePath))
            {
                // Note that this method call requires .NET 4.0 or higher. If using an earlier version it will need to be replaced.
                uploadFileStream.CopyTo(requestStream);
                bytesSent = uploadFileStream.Position;
            }
    
            using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
            {
                bytesReceived = response.ContentLength;
            }
        }
    

提交回复
热议问题