using EnterpriseDT.Net.Ftp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Web;

/// <summary>
/// Nameing 的摘要说明
/// </summary>




    public class UpLoadFile
    {
        FTPConnection ftpConnection;

        public Boolean flag;
        public static string fileName1;
        public static int INV_ORG_ID;
        public UpLoadFile()
        {
        }

        //"192.168.0.187";
        #region 初始化文件传输ftp + void InitFtpConnect()
        /// <summary>
        /// 初始化文件传输ftp 
        /// </summary>
        private void InitFtpConnect()
        {
            flag = false;
            if (this.ftpConnection == null)
            {
                this.ftpConnection = new FTPConnection();
                //this.ftpConnection.ParentControl = this.fQ;
                this.ftpConnection.StrictReturnCodes = true;
                this.ftpConnection.Timeout = 15000;

                //this.ftpConnection.ServerAddress = "120.76.96.55";// "192.168.0.187";//ftpInfo.FtpSeverIP;
                this.ftpConnection.ServerAddress = "101.37.27.113";// "192.168.0.187";//ftpInfo.FtpSeverIP;
                this.ftpConnection.ServerPort = 21;//ftpInfo.FtpPort;
                this.ftpConnection.UserName = "autoorder";// ftpInfo.FtpUser;
                this.ftpConnection.Password = "86435015";// ftpInfo.FtpPassword;
                                                         //this.ftpConnection.DataEncoding = System.Text.Encoding.GetEncoding("UTF-8");
                this.ftpConnection.CommandEncoding = System.Text.Encoding.GetEncoding("GBK");
                this.ftpConnection.Uploaded += new FTPFileTransferEventHandler(Uploaded_Finished);
            }
        }

        string msg = "上传失败";
        int transFileSize = 0;
        
        public string upLoadFile(string type, string path, string servicePath, string JpgPath, string JpgservicePath)
        {


            ftpConnection.UploadFile(path, servicePath, false);//上传  cdr 或pdf
            ftpConnection.UploadFile(JpgPath, JpgservicePath, false);//上传  cdr 或pdf

            return msg;

        }



        //修改名字的方法
        #region
        private void RenameFile(object sender, FTPFileRenameEventArgs e)
        {
            //fileName1 原名包括路径
            //fileName2 新名字

            if (ftpConnection.Exists(fileName1))

                fileName1 = GetNewFileName(fileName1);

            ftpConnection.RenameFile("原名", fileName1);


        }
        #endregion
        #endregion

        void Uploaded_Finished(object objs, FTPFileTransferEventArgs e)
        {
            msg = "上传成功";
            flag = true;
        }
        //开始上传
        public bool uploadFilName(string LocaTionPath, string newFilename)
        {

            InitFtpConnect();
            if (!ftpConnection.IsConnected)
                ftpConnection.Connect();
            //新文件夹名
            this.ftpConnection.UploadFile(LocaTionPath, newFilename);
            this.ftpConnection.Close();
            return flag;
        }

        #region  下载文件
        public void downFile(string filePath)
        {
            InitFtpConnect();
            if (!ftpConnection.IsConnected)
                ftpConnection.Connect();
            this.ftpConnection.DownloadByteArray(filePath);

        }
        #endregion


        //防止文件重命名
        public static string GetNewFileName(string FileName)
        {
            Random rand = new Random();
            string newfilename = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + "m" +
             DateTime.Now.Day.ToString() + "d"
            + DateTime.Now.Second.ToString() + DateTime.Now.Minute.ToString()
            + DateTime.Now.Millisecond.ToString()
                + "a" + rand.Next(1000).ToString()
            + FileName.Substring(FileName.LastIndexOf("."), FileName.Length - FileName.LastIndexOf("."));

            return newfilename;
        }









    }

