Y
YT
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;
}
}
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;
}
}