上传代码如下:
public class Upload : Uploadable
{
[DllImport("wininet.dll")]
private static extern int InternetOpen(string lpszCallerName, int dwAccessType, string lpszProxyName, string lpszProxyBypass, int dwFlags);
[DllImport("wininet.dll")]
private static extern int InternetConnect(int hInternetSession, string lpszServerName, int nProxyPort, string lpszUsername, string lpszPassword, int dwService, int dwFlags, int dwContext);
[DllImport("wininet.dll", CharSet = CharSet.Auto)] //CharSet.Ansi
private static extern bool InternetWriteFile(IntPtr hFile, byte[] lpBuffer, UInt32 dwNumberOfBytesToWrite, ref Int32 lpdwNumberOfBytesWritten);
[DllImport("wininet.dll", CharSet = CharSet.Auto)] //CharSet.Ansi
private static extern int FtpOpenFile(int hConnect, string lpszFileName, ACCESS_TYPE_FOR_FILE dwAccess, ACCESS_TYPE_FOR_TRANSFER dwFlags, UInt32 dwContext);
[DllImport("wininet.dll")]
private static extern int InternetCloseHandle(int hInet);
// These are the generic rights.
private enum ACCESS_TYPE_FOR_FILE : uint
{
GENERIC_READ = 0x80000000,
GENERIC_WRITE = 0x40000000,
GENERIC_EXECUTE = 0x20000000,
GENERIC_ALL = 0x10000000
};
// FTP manifests
private enum ACCESS_TYPE_FOR_TRANSFER
{
FTP_TRANSFER_TYPE_UNKNOWN = 0x00000000,
FTP_TRANSFER_TYPE_ASCII = 0x00000001,
FTP_TRANSFER_TYPE_BINARY = 0x00000002
};
/// <summary>
/// 上传文件(ftp)
/// </summary>
/// <param name="filename">须要上传的文件所在路径</param>
/// <param name="ftpUpFullName">上传文件在FTP上的保存路径和名称</param>
/// <param name="ftpURI">ftp地址</param>
/// <param name="ftpPort">ftp端口</param>
/// <param name="ftpUserID">ftp用户名</param>
/// <param name="ftpPassword">ftp密码</param>
public bool UploadFile(string filename, string ftpUpFullName, string ftpURI, string ftpPort,string ftpUserID, string ftpPassword)
{
try
{
//检查本地文件是否存在
if (File.Exists(filename) == false)
{
return false;
}
//初始FTP连接
int lnghInet = InternetOpen("FtpOperator", 1, null, null, 0);
//连接FTP
int lnghConnect = InternetConnect(lnghInet, ftpURI, Convert.ToInt32(ftpPort), ftpUserID, ftpPassword, 1, 0, 0);
//打开ftp的文件
int hOpen = FtpOpenFile(lnghConnect, ftpUpFullName, ACCESS_TYPE_FOR_FILE.GENERIC_WRITE, ACCESS_TYPE_FOR_TRANSFER.FTP_TRANSFER_TYPE_BINARY, 0);
//读取本地文件到Buf
UInt32 idwNumberOfBytesToWrite = 0;
int ilpdwNumberOfBytesWritte = -1;
FileStream fs2 = new FileStream(@filename, FileMode.Open, FileAccess.Read, FileShare.Read);
idwNumberOfBytesToWrite = (UInt32)fs2.Length;
byte[] dataByte = new byte[idwNumberOfBytesToWrite];
fs2.Read(dataByte, 0, (int)idwNumberOfBytesToWrite);
fs2.Close();
//1、申请一段非托管的内存空间
//IntPtr hglobal = System.Runtime.InteropServices.Marshal.AllocHGlobal(dataByte.Length);
//2、将数据byte[]复制到这段内存空间
//System.Runtime.InteropServices.Marshal.Copy(dataByte, 0, hglobal, dataByte.Length);
//文件上传
bool ret = InternetWriteFile((IntPtr)hOpen, dataByte, idwNumberOfBytesToWrite, ref ilpdwNumberOfBytesWritte);
//long ret = APIs.FtpPutFile(lnghConnect, localFileFullName, ftpUpFullName, 0x80000000, 0);
//上传结果的判断
if (ret == false)
{
return false;
}
//关闭FTP的连接
InternetCloseHandle(lnghInet);
InternetCloseHandle(lnghConnect);
}
catch
{
return false;
}
//返回结果
return true;
}
}
参考: http://blog.csdn.net/oushengfen/article/details/8863009