示例#1
0
        private void miUpload_Click(object sender, EventArgs e)
        {
            //阻止休眠
            CTHelper.PreventSleep();

            //upload files
            pbar1.Maximum = lboxFile.Items.Count;
            pbar1.Value   = 0;

            lbNote.Text = "开始上传文件";
            string logl = CTHelper.CommonPath("app") + "\\upload.log";
            int    countOK = 0, countError = 0;

            foreach (string fileSrc in lboxFile.Items)
            {
                pbar1.Value++;
                lbNote.Text = pbar1.Value + "/" + lboxFile.Items.Count + ", " + countOK + " OK," + countError + " Fail";

                if (checkSkipSameFile.Checked) //重复检查
                {
                    string checkfilename = Path.GetFileName(fileSrc);
                    using (CTDBEntities ct = new CTDBEntities())
                    {
                        var q = from c in ct.tbFile
                                where c.f_pid == ParaDatasetID && c.f_table == ParaTable && c.f_path.Contains(checkfilename)
                                select c;
                        if (q.ToList <tbFile>().Count > 0)
                        {
                            continue;
                        }
                    }
                }

                //check names before upload上传前检测,名称检测
                string fileDes = CTHelper.CommonPath("app") + "\\" + Path.GetFileName(fileSrc).Replace(" ", "");
                if (File.Exists(fileDes))
                {
                    File.Delete(fileDes);
                }
                File.Copy(fileSrc, fileDes, true);
                string md5 = CTHelper.GetMD5Hash(fileDes);
                string fn  = md5 + System.IO.Path.GetExtension(fileDes);

                /////////begin to upload开始上传
                if (File.Exists(fileDes))
                {
                    addFile(fileDes, fileSrc, ParaTable, ParaDatasetID); /////核心上传函数
                }
                ///////////////////

                File.Delete(fileDes);
                this.Enabled = true;
                Application.DoEvents();
            }

            this.Enabled = true;

            //finished
            refreshdb();
            MessageBox.Show("Finishe upload. Error number:" + countError.ToString());
            lbNote.Text = "Finish Upload.";

            //恢复休眠
            CTHelper.RestoreSleep();
        }