FTP trasnferred as binary even with FTP_TRANSFER_TYPE_ASCII specified



Hi,

In my C# code I call WININET's FtpPutFile function to FTP upload text
files. It has a flags parameters to specify whether the transfer
should be done in binary or ASCII. I've specified
FTP_TRANSFER_TYPE_ASCII which is mapped to constant 1, however the
file is still transferred as binary. Could anyone suggest what went
wrong? Thanks!


public class FTPUpload
{
public const int FTP_TRANSFER_TYPE_ASCII = 1;
public const int FTP_TRANSFER_TYPE_BINARY = 2;

private static readonly log4net.Ext.EventID.IEventIDLog evlog =
log4net.Ext.EventID.EventIDLogManager.GetLogger(
System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

[DllImport("WININET", EntryPoint="InternetOpen", SetLastError=true,
CharSet= CharSet.Auto)]
static extern IntPtr InternetOpen (
string agent,
int accessType,
string proxyName,
string proxyBypass,
int flags
);

[DllImport("WININET",
EntryPoint="InternetCloseHandle",SetLastError=true,
CharSet=CharSet.Auto)]
static extern bool InternetCloseHandle(IntPtr hInternet);

[DllImport("WININET",
EntryPoint="InternetConnect",SetLastError=true, CharSet=CharSet.Auto)]
static extern IntPtr InternetConnect (
IntPtr internetHandle,
string serverName,
int serverPort,
string username,
string password,
int service,
int flags,
int context
);


[DllImport("WININET", EntryPoint="FtpSetCurrentDirectory",
SetLastError=true, CharSet=CharSet.Auto)]
static extern bool FtpSetCurrentDirectory (
IntPtr connect,
string directory
);

[DllImport("WININET",
EntryPoint="FtpGetCurrentDirectory",SetLastError=true,
CharSet=CharSet.Auto)]
static extern bool FtpGetCurrentDirectory(
IntPtr connect,
[MarshalAs(UnmanagedType.LPTStr)] StringBuilder buffer,
ref int bufferLength
);

[DllImport("WININET",
EntryPoint="InternetAttemptConnect",SetLastError=true,
CharSet=CharSet.Auto)]
static extern int InternetAttemptConnect(int dwReserved);

[DllImport("WININET", EntryPoint="FtpPutFile",SetLastError=true,
CharSet=CharSet.Auto)]
static extern bool FtpPutFile(
IntPtr connect,
string localFile,
string newRemoteFile,
int flags,
int context
);

[DllImport("WININET", EntryPoint="InternetGetLastResponseInfo",
SetLastError=true, CharSet=CharSet.Auto)]
static extern bool InternetGetLastResponseInfo(
out int error,
[MarshalAs(UnmanagedType.LPTStr)] StringBuilder buffer,
ref int bufferLength
);

static readonly int ERROR_SUCCESS = 0;
static readonly int INTERNET_OPEN_TYPE_DIRECT = 1;
static readonly int INTERNET_SERVICE_FTP = 1;

public static string GetLastFTPError()
{
int errorCode = -1;
int bufferLength = 512;

StringBuilder errorMessage = new StringBuilder(bufferLength);

if(! InternetGetLastResponseInfo(out errorCode,errorMessage,ref
bufferLength))
return "Could not determine error message. ";
else
return errorMessage.ToString();
}

public static void Put(
string ftpServer,
int ftpPort,
string userName,
string password,
string sourceFilePath,
string destinationFileName,
string destinationDirectory,
int flags
)
{
IntPtr inetHandle = IntPtr.Zero;
IntPtr ftpconnectHandle = IntPtr.Zero;

try
{
//check for inet connection
if (InternetAttemptConnect(0) != ERROR_SUCCESS)
{
throw new
ErrorCodeException(EMErrorCode.ecFTPFailedConnectingToInternet,
string.Format("Failed in INet Connection attempt: {0}",
GetLastFTPError()));
}

//connect to inet
inetHandle = InternetOpen("EM Insight
Publishing",INTERNET_OPEN_TYPE_DIRECT,null,null,0);

if (inetHandle == IntPtr.Zero)
{
throw new
ErrorCodeException(EMErrorCode.ecFTPFailedConnectingToInternet,
string.Format("Unable to make INet Connection with error: {0}",
GetLastFTPError()));
}

//connect to ftp
ftpconnectHandle =
InternetConnect(inetHandle,ftpServer,ftpPort,userName,password,INTERNET_SERVICE_FTP,
0,0);

if (ftpconnectHandle == IntPtr.Zero)
{
throw new
ErrorCodeException(EMErrorCode.ecFTPFailedConnectingToFTPServer,
string.Format("Failed on connect to ftp server with error: {0}",
GetLastFTPError()));
}
//set to desired directory on FTP server
if("" != destinationDirectory)
{
if (! FtpSetCurrentDirectory(ftpconnectHandle,
destinationDirectory))
{
throw new
ErrorCodeException(EMErrorCode.ecFTPFailedSettingDestinationDirectory,
string.Format("Couldn't set desired directory on FTP server
with error: {0}", GetLastFTPError()));
}
}

int buffSize = 512;
System.Text.StringBuilder dir = new StringBuilder(buffSize);

//upload file to server
if (! FtpPutFile(ftpconnectHandle, sourceFilePath,
destinationFileName, flags, 0) )
{
throw new ErrorCodeException(EMErrorCode.ecFTPPutFailure,
string.Format("Couldn't upload file to FTP server with error:
{0}", GetLastFTPError()));
}

}
catch(ErrorCodeException ex)
{
evlog.Error("FTP upload failed. ", ex);
throw;
}
catch (Exception ex)
{
ErrorCodeException ecex = new
ErrorCodeException(EMErrorCode.ecFTPFailure, ex);
evlog.Error("Generic FTP upload failure. ", ecex);
throw ecex;
}
finally
{
//close connection to ftp.microsoft.com
if (ftpconnectHandle != IntPtr.Zero)
InternetCloseHandle(ftpconnectHandle);

ftpconnectHandle = IntPtr.Zero;

//close connection to inet
if (inetHandle != IntPtr.Zero)
InternetCloseHandle(inetHandle);

inetHandle = IntPtr.Zero;
}
}

.



Relevant Pages

  • C# FTP
    ... I'm trying to develop a FTP class, ... private const int GENERIC_WRITE = 0x40000000; ... (ByVal hFtpSession As Integer, ByVal lpszFileName As String, ByVal fdwAccess ... private static extern IntPtr FtpOpenFile( ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: FTP trasnferred as binary even with FTP_TRANSFER_TYPE_ASCII specified
    ... You aren't showing the code where you are calling Put and setting the flags. ... int accessType, ... string proxyName, ... static extern bool InternetCloseHandle; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Piped delimited string to int
    ... You can just pipe the enums together and cast the enum to int whenever you ... string and it is int32 hexadecimal format. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Piped delimited string to int
    ... string and it is int32 hexadecimal format. ... enum MyEnum:int ... Clearly the flags are separated by commas rather than "|" in the string ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: That BASH crash, revisited
    ... create a new hash table entry for STRING, ... hash_search (string, table, flags) ... int bucket; ... corruption that happens somewhere in excute_cmd.c: ...
    (comp.os.msdos.djgpp)

Quantcast