【问题标题】:Cannot delete a file on server using ftp from asp.net无法使用 ftp 从 asp.net 删除服务器上的文件
【发布时间】:2020-06-19 08:06:07
【问题描述】:

我创建了一个 asp.net 应用程序来维护购物网站的图像,它包括 2 个功能,一个用于通过 ftp 上传图像,另一个用于使用 ftp 删除图像。

我可以毫无问题地上传文件,但是当我尝试删除文件时,我得到响应“远程服务器返回错误:(530) 未登录。”

我使用的是相同的 ftpuri 和凭据,所以我有点困惑为什么它不起作用。

这是有效的上传代码。

上传部分:

    Dim ftpRequest As FtpWebRequest = CType(WebRequest.Create(ftpuri), FtpWebRequest)

    Try
        ftpRequest.Method = WebRequestMethods.Ftp.UploadFile

        ftpRequest.Credentials = New NetworkCredential(ftpusername, ftppassword)

        Dim bytes() As Byte = System.IO.File.ReadAllBytes(filetoupload)

        ftpRequest.ContentLength = bytes.Length

        Using UploadStream As Stream = ftpRequest.GetRequestStream()
            UploadStream.Write(bytes, 0, bytes.Length)
            UploadStream.Close()
        End Using
    Catch ex As Exception

    End Try

以下是删除失败并出现错误的代码远程服务器返回错误:(530) 未登录。

删除部分:

   Dim ftpRequest As FtpWebRequest = CType(WebRequest.Create(ftpUri), FtpWebRequest)

    Try
        ftpRequest.Credentials = New NetworkCredential(ftpusername, ftppassword)

        ftpRequest.Method = WebRequestMethods.Ftp.DeleteFile

        Dim responseFileDelete As FtpWebResponse = CType(ftpRequest.GetResponse(), FtpWebResponse)

    Catch ex As Exception

    End Try

在这两种情况下,ftpuri、ftpusername 和 ftppassword 中的值都是相同的。

我可以使用具有相同凭据的 ftp 软件删除文件。

任何帮助将不胜感激。

罗恩

【问题讨论】:

    标签: asp.net vb.net ftpwebrequest


    【解决方案1】:

    试试这个方法:

    public bool DeleteFileFromFtpServer(Uri serverUri, string ftpUsername, string ftpPassword)
    {
      try
       {
        // The serverUri should look like this ftp:// scheme.
        // It contains server name along with file name that will be deleted.
        // eg: ftp://abc.com/test.txt. 
    
        if (serverUri.Scheme != Uri.UriSchemeFtp)
        {
            return false;
        }
    
        // Get the object used to communicate with the server.
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
        request.Credentials = new NetworkCredential(ftpUsername, ftpPassword);
        request.Method = WebRequestMethods.Ftp.DeleteFile;
    
        FtpWebResponse response = (FtpWebResponse)request.GetResponse();        
        response.Close();
        return true;
       }
       catch (Exception ex)
        {
          return false;
        }            
    }
    

    调用:

    obj.DeleteFileFromFtpServer(new Uri (toDelFilename), user,pass);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-11
      • 1970-01-01
      相关资源
      最近更新 更多