首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

求改个C#函数替断点续传

2013-06-26 
求改个C#函数为断点续传?求个高手帮忙,,我改了半天好像不行。。。正在找相关例子看,路过的高手给点提示也行//

求改个C#函数为断点续传?
求个高手帮忙,,我改了半天好像不行。。。正在找相关例子看,路过的高手给点提示也行


        /// <summary>
        /// 上传
        /// </summary>
        /// <param name="filename"></param>
        public void Upload(string filename)
        {

            FileInfo fileInf = new FileInfo(filename);
            string uri = ftpURI + fileInf.Name;
            FtpWebRequest reqFTP;

            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
            reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
            reqFTP.KeepAlive = false;
            reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
            reqFTP.UseBinary = true;
            reqFTP.ContentLength = fileInf.Length;
            //记录文件总字节
            ftpUploadFizeSize = fileInf.Length;

            int buffLength = 2048;
            byte[] buff = new byte[buffLength];
            int contentLen;
            FileStream fs = fileInf.OpenRead();
            
            try
            {
                //把上传的文件写入流
                Stream strm = reqFTP.GetRequestStream();
                //每次读文件流的kb
                contentLen = fs.Read(buff, 0, buffLength);
                ftpUploadCurrLen += contentLen;
                //流内容没有结束
                while (contentLen != 0)
                {
                    //把内容从file stream写入upload stream
                    strm.Write(buff, 0, contentLen);


                    contentLen = fs.Read(buff, 0, buffLength);
                    ftpUploadCurrLen += contentLen;
                }
                strm.Close();
                fs.Close();
            }
            catch (Exception ex)
            {
                Insert_Standard_ErrorLog.Insert("FtpWeb", "Upload Error --> " + ex.Message);
            }
        }

        /// <summary>
        /// 下载
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="fileName"></param>
        public void Download(string filePath, string fileName)
        {
            FtpWebRequest reqFTP;
            try
            {
                FileStream outputStream = new FileStream(filePath + "\" + fileName, FileMode.Create);

                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + fileName));
                reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
                reqFTP.UseBinary = true;
                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                Stream ftpStream = response.GetResponseStream();
                long cl = response.ContentLength;
                int bufferSize = 2048;
                int readCount;
                byte[] buffer = new byte[bufferSize];

                readCount = ftpStream.Read(buffer, 0, bufferSize);


                while (readCount > 0)
                {
                    outputStream.Write(buffer, 0, readCount);
                    readCount = ftpStream.Read(buffer, 0, bufferSize);
                }

                ftpStream.Close();
                outputStream.Close();
                response.Close();
            }
            catch (Exception ex)
            {
                Insert_Standard_ErrorLog.Insert("FtpWeb", "Download Error --> " + ex.Message);
            }
        }


[解决办法]
断点续传主要是AddRange方法来告诉服务器你需要从哪个位置开始下载的了,希望下面的文章可以帮助到你:
http://www.cnblogs.com/zhili/archive/2013/05/11/EAP.html
博客园也有很多相关的文章,你直接搜索 断点续传就可以看到好多的
[解决办法]
这个,取决于你的服务器是否支持断点。

热点排行