如何在FTP服务器上创建目录 [英] How to create directory on FTP server

查看:294
本文介绍了如何在FTP服务器上创建目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码在 FTP 服务器上创建文件夹;但它在我的情况下不起作用:-

            Dim sFilePath as string =filepath
            Dim ftpResponse1 As FtpWebResponse
            Dim ftpRequest1 As FtpWebRequest
            Dim IsExists1 As Boolean = True
            ftpRequest1 = CType(FtpWebRequest.Create(sFilePath), FtpWebRequest)
            ftpRequest1.UseBinary = True
            ftpRequest1.Credentials = New NetworkCredential(ZXFTPUSER, ZXFTPPASS)
            ftpRequest1.UsePassive = True
            ftpRequest1.Method = WebRequestMethods.Ftp.MakeDirectory
            'ftpRequest1.KeepAlive = False
            'ftpResponse1 = ftpRequest1.GetResponse()

            'ftpResponse1 = ftpRequest1.GetResponse()
            'Dim strstream1 As Stream = ftpResponse1.GetResponseStream()
            'Dim strreader1 As New StreamReader(strstream1)
            'Console.WriteLine(strreader1.ReadToEnd())
            'strreader1.Close()
            'strstream1.Close()
            'ftpResponse1.Close()

请帮帮我.

在上述情况下,我没有收到任何错误,但是当我要上传 rar 文件时,出现以下异常

In the above case i am not getting any error but when i am going to upload a rar file then it is giving the following exception

远程服务器返回错误:(550) 文件不可用(例如,找不到文件,无法访问).

The remote server returned an error: (550) File unavailable (e.g., file not found, no access).

文件上传代码如下

 Public Sub FTPUpload(ByVal SourceFile() As IO.FileInfo, ByVal folderLevel As Integer, ByVal ftpPassiveMode As Boolean)
            ZXFTPPASS = "******"
            Dim filePath As New IO.DirectoryInfo(filePaths)
            Dim ftpRequest As FtpWebRequest
            Dim dResult As Windows.Forms.DialogResult
            Dim ftpFilePath As String = ""
            Dim levelPath As String = ""
            Dim iLoop As Integer
            Dim uFile As IO.FileInfo

            For Each uFile In SourceFile
                Try
                    ftpFilePath = levelPath & "/" & uFile.Name
                    ftpRequest = CType(FtpWebRequest.Create(ftpFilePath), FtpWebRequest)

                    ftpRequest.Credentials = New NetworkCredential(ZXFTPUSER, ZXFTPPASS)
                    ftpRequest.UsePassive = ftpPassiveMode
                    ftpRequest.UseBinary = True
                    ftpRequest.KeepAlive = False
                    ftpRequest.Method = WebRequestMethods.Ftp.UploadFile
                    'Read in the file
                    Dim b_file() As Byte = System.IO.File.ReadAllBytes(filePath.FullName & "\" & uFile.Name.ToString())

                    'Upload the file
                    Dim cls_stream As Stream = ftpRequest.GetRequestStream()
                    cls_stream.Write(b_file, 0, b_file.Length)
                    cls_stream.Close()
                    cls_stream.Dispose()

                    'MsgBox("Uploaded Successfully", MsgBoxStyle.Information)
                Catch
                    MsgBox("Failed to upload.Please check the ftp settings", MsgBoxStyle.Critical)
                End Try
            Next
        End Sub

推荐答案

浏览各种网站,我发现:

Looking through various sites I have found this:

Private Function FtpFolderCreate(folder_name As String, username As String, password As String) As Boolean
    Dim request As Net.FtpWebRequest = CType(FtpWebRequest.Create(folder_name), FtpWebRequest)
    request.Credentials = New NetworkCredential(username, password)
    request.Method = WebRequestMethods.Ftp.MakeDirectory

    Try
        Using response As FtpWebResponse = DirectCast(request.GetResponse(), FtpWebResponse)
            ' Folder created
        End Using
    Catch ex As WebException
        Dim response As FtpWebResponse = DirectCast(ex.Response, FtpWebResponse)
        ' an error occurred
        If response.StatusCode = FtpStatusCode.ActionNotTakenFileUnavailable Then
            Return False
        End If
    End Try
    Return True
End Function

这篇关于如何在FTP服务器上创建目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆