Beispiel #1
0
        /// <summary>
        /// tabUpload页面上传接口
        /// </summary>
        /// <param name="pathName">本地文件或文件夹全路径名称</param>
        /// <param name="remoteDirPath">远程文件夹路径</param>
        /// <param name="dirProgressAction">进度条代理(arg0:已上传字节数/共需上传字节数=0.6678)</param>
        /// <param name="speedAction">上传速度代理(arg0:字节数/上传时间(B/s))</param>
        /// <param name="timeAction">剩余时间代理(arg0:还未上传字节数/上传速度(Datetime))</param>
        /// <param name="numAction">上传文件个数代理(arg0:已上传文件数,arg1:共需上传文件数)</param>
        /// <param name="logAction">上传日志代理(arg0:日志记录(string))</param>
        /// <returns></returns>
        public static void uploadPath(
            string pathName,
            Action <double> dirProgressAction = null,
            Action <double> speedAction       = null,
            Action <DateTime> timeAction      = null,
            Action <int, int> numAction       = null,
            Action <string> logAction         = null,
            Action <bool> statusAction        = null
            )
        {
            totalPathName = pathName;

            //遍历文件夹获取所有文件的信息
            var fileInfos = PathUtils.getFileInfos(pathName);

            //totalNum = 总文件个数
            fileNum = fileInfos.Count;
            //totalSize = 总文件大小
            dirSize = PathUtils.getTotalFileSize(fileInfos);
            //uploadedNum = 已上传文件个数
            int uploadedFileNum = 0;


            if (File.Exists(pathName))
            {
                pathName = Path.GetDirectoryName(pathName);
            }

            FtpPathHash = "/opt/ftp/cxy/" + HashUtils.getHashName(pathName); //解析参数

            HashUtils.setLocalParentPath(pathName);                          //设置服务器根目录 的地址



            setRemoteDir(pathName);
            Console.Out.WriteLine("create Path name:", pathName);

            if (numAction != null)
            {
                numAction(uploadedFileNum, fileNum);
            }

            if (logAction != null)
            {
                logAction("------ 上传文件流程开始 ------");
            }
            if (logAction != null)
            {
                logAction("***********         ***********");
            }
            //上传前先检查文件夹是否存在
            foreach (FileInfo fileInfo in fileInfos)
            {
                //LogAction:上传文件开始
                if (logAction != null)
                {
                    logAction("文件 " + fileInfo.Name + " 开始上传 ");
                }
                uploadFile(fileInfo, null, speedAction, timeAction, dirProgressAction);
                //LogAction:上传文件结束
                if (logAction != null)
                {
                    logAction("文件 " + fileInfo.Name + " 上传完成 ");
                }
                //已上传文件数+1
                if (numAction != null)
                {
                    numAction(++uploadedFileNum, fileNum);
                }
            }
            if (logAction != null)
            {
                logAction("***********         ***********");
            }
            if (logAction != null)
            {
                logAction("------ 上传文件流程完成 ------");
            }
            #region 文件传输完成后UI操作

            if (dirProgressAction != null)
            {
                //单文件上传进度更新为1
                dirProgressAction(1.0);
            }
            //if (speedAction != null)
            //上传速度更新为0
            // speedAction(0.0);
            if (timeAction != null)
            {
                timeAction(Convert.ToDateTime("1900-01-01 00:00:00"));
            }
            #endregion
            if (statusAction != null)
            {
                statusAction(true);
            }
            reset();
        }
Beispiel #2
0
        /// <summary>
        /// 上传文件到FTP服务器(断点续传)
        /// </summary>
        /// <param name="localFullPath">本地文件对象</param>
        /// <param name="remoteDirPath">远程文件夹路径</param>
        /// <param name="fileProgressAction">报告进度的处理(第一个参数:总大小,第二个参数:当前进度)</param>
        /// <returns></returns>
        public static void uploadFile(FileInfo fileInfo,
                                      Action <double> fileProgressAction = null,
                                      Action <double> speedAction        = null,
                                      Action <DateTime> timeAction       = null,
                                      Action <double> dirProgressAction  = null)
        {
            //比较已上传大小
            long fileSize = (long)fileInfo.Length;

            uploadedFileSize = getFileSize(fileInfo);
            // Console.Out.WriteLine(initialSize);
            // Console.Out.WriteLine("initialSize:" + initialSize);
            //如果ftp服务器上文件大小已超过本地文件大小,返回false
            if (uploadedFileSize > fileSize)
            {
                return;
            }

            //初始化进度条
            if (dirProgressAction != null)
            {
                curUploadedDirSize += uploadedFileSize;
                dirProgressAction((double)curUploadedDirSize / (double)dirSize);
                //dbAction(curUploadedDirSize);
                //文件已上传完毕
                if (uploadedFileSize == fileSize)
                {
                    return;
                }
            }

            //请求设置
            string uri     = HashUtils.getFileUri(fileInfo.FullName);
            var    request = getFtpWebRequest(new Uri(uri), WebRequestMethods.Ftp.AppendFile);


            // 上传文件时通知服务器文件的大小
            request.ContentLength = fileInfo.Length;

            //缓存和流设置
            int bufferSize = 1048576;// 缓冲大小设置为2kb

            byte[] buffer = new byte[bufferSize];

            using (FileStream fileStream = fileInfo.OpenRead())
            {
                fileStream.Seek(uploadedFileSize, 0);
                using (Stream uploadStream = request.GetRequestStream())
                {
                    while (true)
                    {
                        int blockSize = fileStream.Read(buffer, 0, bufferSize);
                        if (blockSize == 0)
                        {
                            break;
                        }

                        // 把内容从file stream 写入 upload stream
                        uploadStream.Write(buffer, 0, blockSize);
                        uploadedFileSize   += blockSize;
                        curUploadedDirSize += blockSize;
                        //当时间间隔>1000ms,更新进度

                        DateTime curTime  = DateTime.Now;
                        TimeSpan timespan = curTime.Subtract(prevTime);
                        if (timespan.TotalMilliseconds > 1000)
                        {
                            prevTime = curTime;
                            //更新进度
                            if (fileProgressAction != null)
                            {
                                fileProgressAction((double)uploadedFileSize / (double)fileSize);//更新进度条
                            }
                            if (speedAction != null)
                            {
                                speedAction((double)(curUploadedDirSize - prevUploadedDirSize) / (double)(timespan.TotalMilliseconds) * 1000.0);
                            }
                            if (timeAction != null)
                            {
                                var      speed      = (double)(curUploadedDirSize - prevUploadedDirSize) / (double)(timespan.TotalMilliseconds) * 1000.0;
                                var      seconds    = (dirSize - curUploadedDirSize) / speed;
                                DateTime remainTime = Convert.ToDateTime("1900-01-01 00:00:00");
                                remainTime = remainTime.AddSeconds(seconds);
                                timeAction(remainTime);
                            }
                            if (dirProgressAction != null)
                            {
                                dirProgressAction((double)curUploadedDirSize / (double)dirSize);
                            }
                            prevUploadedDirSize = curUploadedDirSize;
                        }
                    }
                }
            }
            #region 文件传输完成后UI操作

            /*
             * if (fileProgressAction != null)
             *  //单文件上传进度更新为1
             *  fileProgressAction(1.0);
             * if (speedAction != null)
             *  //上传速度更新为0
             *  speedAction(0.0);
             */
            #endregion
        }