Special characters in FTP files

I am having problems managing files on FTP when they have special characters. For example, file names with óor analogies.

I will give you an example. First, I want to list and process each folder file in FTP:

ftp = CType(FtpWebRequest.Create(sFtpPath), FtpWebRequest)
ftp.Method = WebRequestMethods.Ftp.ListDirectory
reader = New StreamReader(ftp.GetResponse().GetResponseStream())

files = reader.ReadToEnd.Split(New String() {NewLine}, StringSplitOptions.RemoveEmptyEntries)
reader.Close()

But this gives me problems when the file contains special characters, because the line that I have for the file does not exist on FTP, for example:

For Each sFich As String In files
    ftp = CType(FtpWebRequest.Create(sFtpPath & "/" & sFich), FtpWebRequest)
    ftp.Method = WebRequestMethods.Ftp.DownloadFile
    reader = New StreamReader(ftp.GetResponse().GetResponseStream())

    '...
Next

For example, a file EXAMPLE_aróon FTP is retrieved as here EXAMPLE_ar□, so when I try to upload a file, it says it does not exist.

How can I handle this?

+5
source share
3 answers

, StreamReader, , . UTF8, :

reader = New StreamReader(ftp.GetResponse().GetResponseStream(), System.Text.Encoding.Unicode)

, , , .

00F3. , . , Google , FTP- EBCDIC, 1145. :

reader = New StreamReader(ftp.GetResponse().GetResponseStream(), System.Text.Encoding.GetEncoding(1145))

Edit: , F3 , ASCII, EBCDIC, .

+2

, . listDir ... , ...

 Public Shared Function DownloadFileFromServer(fileUri As Uri) As Boolean

    If fileUri.Scheme <> Uri.UriSchemeFtp Then
        Return False
    End If
    ' Get the object used to communicate with the server.
    Dim request As New WebClient()


    request.Credentials = New NetworkCredential("user", "pass")
    Try
        Dim newFileData As Byte() = request.DownloadData(fileUri.ToString())
        Dim fileString As String = System.Text.Encoding.UTF8.GetString(newFileData)
        'do something with data
        Console.WriteLine(fileString)
    Catch e As WebException
        Console.WriteLine(e.ToString())
    End Try
    Return True
End Function

Public Shared Function listDir() As Boolean

    Dim request As FtpWebRequest = DirectCast(WebRequest.Create("ftp://www.yourftpsite.com/docs/"), FtpWebRequest)
    request.Method = WebRequestMethods.Ftp.ListDirectoryDetails

    request.Credentials = New NetworkCredential("user", "pass")

    Dim response As FtpWebResponse = DirectCast(request.GetResponse(), FtpWebResponse)

    Dim responseStream As Stream = response.GetResponseStream()
    Dim reader As New StreamReader(responseStream)

    Dim dirlist As String() = Nothing
    dirlist = reader.ReadToEnd().Split(New String() {vbCr & vbLf}, StringSplitOptions.RemoveEmptyEntries)

    For Each dirLine As String In dirlist

        Dim dirData As String = dirLine

        ' Parse date
        Dim lineDate As String = dirData.Substring(0, 17)
        Dim dateTime__1 As DateTime = DateTime.Parse(lineDate)
        dirData = dirData.Remove(0, 24)

        ' check if line is a directory
        Dim dir As String = dirData.Substring(0, 5)
        Dim isDirectory As Boolean = dir.Equals("<dir>", StringComparison.InvariantCultureIgnoreCase)
        dirData = dirData.Remove(0, 5)
        dirData = dirData.Remove(0, 10)

        ' get the filename
        If Not isDirectory Then
            Dim fileName As String = dirData

            'the actual filename
            Console.WriteLine("name= " & fileName)

        End If
    Next

    reader.Close()
    response.Close()

    Return True

End Function
0

, , - , # hash/number.

( Microsoft Online Community). Uri.EscapeDataString(). , :

. , :

string FTPFilePath = "ftp://myserverip.com/Beatles #1 Hits/Help document.mp3";
if ((FTPFilePath).IndexOf("#") > -1){
 targetUri = new Uri(Uri.EscapeDataString(FTPFilePath));
}
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(targetUri);

:

Dim ftpFilePath = "ftp://myserverip.com/Beatles #1 Hits/Help document.mp3"

Dim parsedUri = new Uri(ftpFilePath)
Dim scheme = parsedUri.Scheme
Dim host = parsedUri.Host
Dim port = parsedUri.Port
Dim pathThatMightHaveHashSigns = ftpFilePath.Replace(String.Format("{0}://{1}", scheme, host), String.Empty)

'Dim targetUri = new Uri(Uri.EscapeDataString(ftpFilePath))
' The above line would throw a UriFormatException "Invalid URI: The format of the URI could not be determined."

Dim targetUri = new UriBuilder(scheme, host, port, pathThatMightHaveHashSigns).Uri
Dim request = CType(WebRequest.Create(targetUri), FtpWebRequest)

... #:

var ftpFilePath = "ftp://myserverip.com/Beatles #1 Hits/Help document.mp3";

var parsedUri = new Uri(ftpFilePath);
var scheme = parsedUri.Scheme;
var host = parsedUri.Host;
var port = parsedUri.Port;
var pathThatMightHaveHashSigns = ftpFilePath.Replace(String.Format("{0}://{1}", scheme, host), String.Empty);

//var targetUri = new Uri(Uri.EscapeDataString(ftpFilePath));
// The above line would throw a UriFormatException "Invalid URI: The format of the URI could not be determined."

var targetUri = new UriBuilder(scheme, host, port, pathThatMightHaveHashSigns).Uri;
targetUri.Dump();
0

All Articles