示例#1
0
        /// <summary>
        /// 上传具体方法,根据关联的项目数循环上传至FTP
        /// </summary>
        /// <param name="filename">被上传的文件路径</param>
        /// <param name="serverpath">上传的项目所在的相对路径</param>
        /// <param name="pp">进度条控件</param>
        private void Upload1(string filename, string serverpath, ProgressBar pp)
        {
            //读取区间大小
            int buffLength = 2048;

            byte[]         buffer;
            FtpWebRequest  reqFTP;
            FtpWebResponse uploadResponse = null;
            //上传数据流
            Stream strm = null;
            //本地文件流
            FileStream fs = null;

            //获取文件信息
            FileInfo fileInf = new FileInfo(filename);

            try {
                if (!fileInf.Exists)
                {
                    MessageBox.Show("文件不存在", "提示");
                    return;
                }
            } catch (Exception e1) {
                MessageBox.Show(e1.ToString(), "提示");
                return;
            }


            ListViewItem item = (ListViewItem)pp.Tag;

            LinkLabel        l            = ((LinkLabel)((EXControlListViewSubItem)item.SubItems[4]).MyControl);
            del_do_update    delupdate    = new del_do_update(do_update);
            del_do_changetxt delchangetxt = new del_do_changetxt(ChangeTextMethod);

            //查询要上传至服务器的项目数
            List <ProjectInfo> _prjlist = new List <ProjectInfo>();

            if (!string.IsNullOrEmpty(item.SubItems[5].Text))
            {
                _prjlist = ProjectInfoDao.getAllProjectInfoByPackID(new string[] { item.SubItems[5].Text });
            }

            for (int i = 0; i < _prjlist.Count; i++)
            {
                if (!bool.Parse(item.Tag as string))               //中断
                {
                    break;
                }
                try {
                    this.MakeDirectory(ftphost + _prjlist[i].Ftppath + serverpath);
                } catch (Exception) {
                    MessageBox.Show("创建文件夹错误,可能目标文件夹已经存在", "提示");
                }

                string uri = ftphost + _prjlist[i].Ftppath + serverpath + fileInf.Name;
                try {
                    // 根据uri创建FtpWebRequest对象
                    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
                    // ftp用户名和密码
                    reqFTP.Credentials = new NetworkCredential(this.username, this.password);
                    reqFTP.Proxy       = GlobalProxySelection.GetEmptyWebProxy();
                    // 默认为true,连接不会被关闭
                    // 在一个命令之后被执行
                    reqFTP.KeepAlive = false;
                    // 指定执行什么命令
                    reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
                    // 指定数据传输类型
                    reqFTP.UseBinary = true;
                    // 上传文件时通知服务器文件的大小
                    reqFTP.ContentLength = fileInf.Length;
                    // 缓冲大小设置为2kb
                    buffer = new byte[buffLength];
                    // 打开一个文件流 (System.IO.FileStream) 去读上传的文件
                    fs   = fileInf.OpenRead();
                    strm = reqFTP.GetRequestStream();
                    int bytesRead;
                    int hasread = 0;
                    pp.BeginInvoke(delupdate, new object[] { pp, hasread, (int)fileInf.Length, i + 1, _prjlist.Count, _prjlist[i].Projectname });
                    while (bool.Parse(item.Tag as string))                    //中断
                    {
                        //Thread.Sleep(300);
                        bytesRead = fs.Read(buffer, 0, buffer.Length);
                        hasread  += bytesRead;
                        if (bytesRead == 0)
                        {
                            break;
                        }
                        strm.Write(buffer, 0, bytesRead);
                        pp.BeginInvoke(delupdate, new object[] { pp, hasread, (int)fileInf.Length, i + 1, _prjlist.Count, _prjlist[i].Projectname });
                    }
                    fs.Close();
                    strm.Close();
                    uploadResponse = (FtpWebResponse)reqFTP.GetResponse();
                } catch (Exception e) {
                    MessageBox.Show("上传到" + uri + "发生错误:" + e.ToString(), "提示");
                    pp.BeginInvoke(delupdate, new object[] { pp, (int)fileInf.Length, (int)fileInf.Length, i + 1, _prjlist.Count, _prjlist[i].Projectname });
                }
            }
            l.BeginInvoke(delchangetxt, new object[] { l, "重传" });

            if (uploadResponse != null)
            {
                uploadResponse.Close();
            }
            if (fs != null)
            {
                fs.Close();
            }
            if (strm != null)
            {
                strm.Close();
            }
        }