Ejemplo n.º 1
0
        private void btnUpload_Click(object sender, EventArgs e)
        {
            btnUpload.Enabled = false;
            btnPause.Enabled  = true;
            chkAll.Enabled    = false;
            btnAbort.Enabled  = false;
            try
            {
                //SetSelectCellStatus(true);
                dgvList.Enabled = false;
                if (btnUpload.Text.ToUpper().EndsWith("UPLOAD"))
                {
                    lblStatus.Text = "Saving the upload list, please wait...";
                    Application.DoEvents();
                    try
                    {
                        if (callbyPause == false)
                        {
                            DB.DeleteAllUnfinishedUploadItem(sitename);
                            foreach (DataGridViewRow dgvr in dgvList.Rows)
                            {
                                if ((bool)dgvr.Cells[1].Value == true)
                                {
                                    if (dgvr.Cells["Status"].Value.ToString() == "" || dgvr.Cells["Status"].Value.ToString().StartsWith("<!>"))
                                    {
                                        DB.SaveUploadlist(sitename, dgvr.Cells["LP"].Value.ToString(), dgvr.Cells["FileName"].Value.ToString(),
                                                          lblFtpPath.Text, dgvr.Cells["Type"].Value.ToString(), dgvr.Cells["Size"].Value.ToString(),
                                                          dgvr.Cells["FileFullName"].Value.ToString(), dgvr.Cells["FtpFullFileName"].Value.ToString(), dgvr.Cells["ModifyDate"].Value.ToString());
                                    }
                                }
                                //dgvr.Cells["Status"].Value = "";
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }

                    lblStatus.Text = "Start uploading...";
                    if (callbyPause == false)
                    {
                        lblFinished.Text = "0";
                    }
                    else
                    {
                        callbyPause = false;
                    }

                    //向上滚动DGV
                    int idgvX = 0, idgvY = 0, iCellX = 0, iCellY = 0;
                    idgvX = dgvList.Location.X;
                    idgvY = dgvList.Location.Y;
                    bool b_Move    = false;
                    int  iRowCount = -1;
                    dgvList.FirstDisplayedScrollingRowIndex = 0;

                    for (int i = 0; i < dgvList.Rows.Count; i++)
                    {
                        if (btnPause.Text == "Start")
                        {
                            break;
                        }
                        #region //向上滚动
                        if (b_Move)
                        {
                            dgvList.FirstDisplayedScrollingRowIndex = dgvList.Rows[i].Index - iRowCount;
                        }
                        iCellX = dgvList.GetCellDisplayRectangle(0, dgvList.Rows[i].Index, false).X;
                        iCellY = dgvList.GetCellDisplayRectangle(0, dgvList.Rows[i].Index, false).Y;

                        //如果得到的单元格的Y坐标=0了,说明到底了,要开始往上移动
                        //如果得到的单元格的Y坐标加上单元格高度比控件高度高了,要开始往上移动
                        //一旦开始移动,则每个循环都要移动了
                        //2倍的单元格高度,因为到底后可能出现横向滚动条,多以提早开始移动
                        if (iCellY == 0 || idgvY + iCellY + dgvList.Rows[i].Cells[0].Size.Height + dgvList.Rows[i].Cells[0].Size.Height > idgvY + dgvList.Height)
                        {
                            dgvList.FirstDisplayedScrollingRowIndex = dgvList.Rows[i].Index - iRowCount;
                            iCellY = dgvList.GetCellDisplayRectangle(0, dgvList.Rows[i].Index, false).Y;
                            b_Move = true;
                        }
                        else
                        {
                            if (b_Move == false)
                            {
                                iRowCount++;
                            }
                        }
                        #endregion

                        if ((bool)dgvList.Rows[i].Cells[1].Value == true
                            &&
                            (dgvList.Rows[i].Cells["Status"].Value.ToString().StartsWith("<OK>") == false && dgvList.Rows[i].Cells["Status"].Value.ToString().StartsWith("<ERROR>") == false))
                        {
                            try
                            {
                                ResetStructCurrentUpload();
                                currentuploadinfo.SiteName            = sitename;
                                currentuploadinfo.LocalPath           = dgvList.Rows[i].Cells["LP"].Value.ToString();
                                currentuploadinfo.FileNameWithoutPath = dgvList.Rows[i].Cells["FileName"].Value.ToString();
                                currentuploadinfo.FtpPath             = lblFtpPath.Text;
                                currentuploadinfo.Type            = dgvList.Rows[i].Cells["Type"].Value.ToString();
                                currentuploadinfo.FileSize        = Convert.ToInt32(dgvList.Rows[i].Cells["Size"].Value);
                                currentuploadinfo.FtpFullFileName = dgvList.Rows[i].Cells["FtpFullFileName"].Value.ToString();

                                if (currentuploadinfo.Type == "File Folder")
                                {
                                    MakeFolder(currentuploadinfo.FtpFullFileName);

                                    DB.DeleteUploaditem(currentuploadinfo.SiteName, currentuploadinfo.LocalPath, currentuploadinfo.FileNameWithoutPath);
                                    dgvList.Rows[i].Cells[0].Value        = res.ok;
                                    dgvList.Rows[i].Cells["Status"].Value = "<OK>Finished";
                                }
                                else
                                {
                                    if (renamefile == "Y")
                                    {
                                        currentuploadinfo.CurrentUsedFtpFullFileName = dgvList.Rows[i].Cells["FtpFullFileName"].Value.ToString() + ".tmp";
                                    }
                                    else
                                    {
                                        currentuploadinfo.CurrentUsedFtpFullFileName = dgvList.Rows[i].Cells["FtpFullFileName"].Value.ToString();
                                    }
                                    dgvList.Rows[i].Cells[0].Value = res.upload;

                                    int finishedlength = 0;
                                    if (dgvList.Rows[i].Cells["Status"].Value.ToString() != "")
                                    {
                                        if (type == "FTP")
                                        {
                                            using (DataTable dtOnefile = Ftp.GetFileList(currentuploadinfo.CurrentUsedFtpFullFileName))
                                            {
                                                if (dtOnefile != null && dtOnefile.Rows.Count > 0)
                                                {
                                                    finishedlength = Convert.ToInt32((dtOnefile.Rows[0]["Size"]).ToString());
                                                }
                                            }
                                        }
                                        else
                                        {
                                            using (DataTable dtOnefile = sFtp.GetFileList(currentuploadinfo.CurrentUsedFtpFullFileName))
                                            {
                                                if (dtOnefile != null && dtOnefile.Rows.Count > 0)
                                                {
                                                    finishedlength = Convert.ToInt32((dtOnefile.Rows[0]["Size"]).ToString());
                                                }
                                            }
                                        }
                                    }

                                    if (currentuploadinfo.FtpFullFileName.Length > currentuploadinfo.FileNameWithoutPath.Length)
                                    {
                                        string ftpfilepath = currentuploadinfo.FtpFullFileName.Substring(0, currentuploadinfo.FtpFullFileName.Length - currentuploadinfo.FileNameWithoutPath.Length - 1);

                                        MakeFolder(ftpfilepath);
                                    }
                                    if (type == "FTP")
                                    {
                                        Ftp.Upload(this, i, ftppathwithoutip,
                                                   dgvList.Rows[i].Cells["FileFullName"].Value.ToString(),
                                                   initiallocalpath, finishedlength);
                                    }
                                    else
                                    {
                                        sFtp.Upload(this, i, ftppathwithoutip,
                                                    dgvList.Rows[i].Cells["FileFullName"].Value.ToString(),
                                                    initiallocalpath, finishedlength);
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                dgvList.Rows[i].Cells[0].Value        = res.error;
                                dgvList.Rows[i].Cells["Status"].Value = "<ERROR>" + ex.Message;
                                DB.DeleteUploaditem(currentuploadinfo.SiteName, currentuploadinfo.LocalPath, currentuploadinfo.FileNameWithoutPath);
                            }
                            finally
                            {
                                lblFinished.Text = (Convert.ToInt32(lblFinished.Text) + 1).ToString();
                                Application.DoEvents();
                            }
                        }
                    }
                }
            }
            finally
            {
                if (btnPause.Text != "Start")
                {
                    //SetSelectCellStatus(false);
                    dgvList.Enabled   = true;
                    btnPause.Enabled  = false;
                    btnUpload.Enabled = true;
                    chkAll.Enabled    = true;
                    btnAbort.Enabled  = true;
                    MessageBox.Show("Upload finished", "Upload", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }