Archived Forum Post

Index of archived forum posts

Question:

Sftp WriteFileBytes writes extra space characters in the file

Sep 24 '14 at 09:59

Using Chilkat.Sftp dll, I am writing .csv file in the Linux Server.

I am using WriteFileBytes Function to write the file.

Function WriteFileBytes(ByVal handle As String, ByVal byteData As Byte()) As Boolean

Just noticed that, this function writes space characters along with the existing column values. Our database application which imports data from the csv file throws exception. After debugging, we found that there is additional space characters on the file.

How to fix this ? Do i need to use WriteFileText or any other WriteByte Method or WriteBinaryFile to overcome this error ?


Accepted Answer

The FTP protocol has some options to upload files in text mode (convert crlf -> lf) and you probably have those options set in Chilkat. The SFTP protocol does not have such an option. Most likely the source file you are uploading has CRLF bytes for line-ending. You will need to remove them yourself before you upload to an SFTP server.


Answer

The WriteFileBytes method sends the exact bytes to the SFTP server with no modifications. It does not examine that bytes that you pass to it.

Please think about this for a moment: Does it seem likely that for some insane reason WriteFileBytes will attempt to analyze the bytes that are passed to it, somehow recognize that it is CSV, insert SPACE characters are regular points within the CSV, and then send it?


Answer

Issue is, .csv file written by Chilkat.Sftp WriteByte Method has CRLF at the end of everyline. When i do it through FTP, i have LF in the end of every line.alt text


Answer

When you call sftp.OpenFile, examine the value of the last argument (createDisposition) that you are passing. Refer to the online reference documentation for information about it.

Also, given that you must have code that opens the .csv, and reads its contents into memory, check to make sure the LF to CRLF conversion did not happen in the code prior to calling WriteFileBytes


Answer

Thank you Chilkat. Issue is solved. When the .csv file is uploaded, I am validating the .csv file by recreating a Temp .csv file in the Temp folder.

I recreate it by reading the data from .csv file and ensure about CR LF or LF.

Below syntax, validates the .csv file and append LF in the end of row. No Carriage Return.

This works for either FTP or SFTP.

Dim strPath As String = String.Empty
strPath = "C:\Windows\Temp\" & _file_Name

Try

Dim reader As System.IO.StreamReader = New System.IO.StreamReader(System.IO.File.OpenRead(.txtFileName.Text.Trim()))

Dim listA As New List(Of String)()

If System.IO.File.Exists(strPath) Then
System.IO.File.Delete(strPath)
End If

Dim sw As New System.IO.StreamWriter(strPath)
Dim s As String = String.Empty

While reader.Peek() >= 0
Dim line As String = reader.ReadLine()
       Dim values As String() = line.Split(";"c)
       listA.Add(values(0))
       s = s + line + Chr(10)
End While

reader.Close()
sw.Write(s)
sw.Close()

Catch ex As Exception
MessageBox.Show(ex.Message)
End Try