Esempio n. 1
0
        /// <summary>
        /// 获取已上传文件大小
        /// </summary>
        /// <param name="localFileFullPathName">本地文件全路径</param>
        /// <returns></returns>
        public static long getFileSize(FileInfo fileInfo)
        {
            string uri = HashUtils.getFileUri(fileInfo.FullName);

            Console.WriteLine("-------------文件在服务器的地址  uri:  " + uri);
            // 根据uri创建FtpWebRequest对象
            var request = getFtpWebRequest(new Uri(uri), WebRequestMethods.Ftp.GetFileSize);

            try
            {
                FtpWebResponse response = (FtpWebResponse)request.GetResponse();
                long           filesize = response.ContentLength;
                return(filesize);
            }
            catch (Exception ex)
            {
                return(0);
            }
        }
Esempio n. 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
        }