WinCE上将文件上传至FTP服务器

上传代码如下:

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值