Example #1
0
        private void GetFiles(string dir)
        {
            int dirLen = dir.Length;

            string[] files = Directory.GetFiles(dir, "*.*", SearchOption.AllDirectories);
            foreach (string file in files)
            {
                FileInfo fi = new FileInfo(file);
                KellFileTransfer.FILELIST f = new KellFileTransfer.FILELIST();
                f.文件路径 = fi.FullName.Substring(dirLen + 1);
                f.文件大小 = fi.Length;
                try
                {
                    string fullname = this.dir + "\\" + f.文件路径;
                    string newDir   = Path.GetDirectoryName(fullname);
                    if (!Directory.Exists(newDir))
                    {
                        Directory.CreateDirectory(newDir);
                    }
                    fi.CopyTo(fullname, true);
                    list.Add(f);
                }
                catch (Exception e)
                {
                    if (showErrMsg && MessageBox.Show("该文件[" + file + "]可能正被其他进程占用!无法添加。" + Environment.NewLine + e.Message, "是否继续显示此错误信息?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != System.Windows.Forms.DialogResult.OK)
                    {
                        showErrMsg = false;
                    }
                }
            }
        }
Example #2
0
 public static void UploadAttachment(string localFilename, User user)
 {
     try
     {
         KellFileTransfer.FILELIST file = KellFileTransfer.Common.GetFILE(localFilename);
         if (KellFileTransfer.FileUploader.SendFile(localFilename, KellFileTransfer.Common.GetUploadIPEndPoint()))
         {
             Attachment attch = new Attachment();
             attch.AttachmentFilename = file.文件路径;
             attch.Size     = file.文件大小;
             attch.Uploader = user;
             int    r = AttachmentLogic.GetInstance().AddAttachment(attch);
             string s = r > 0 ? "":",但保存到数据库失败!";
             MessageBox.Show("上传成功!" + s);
         }
         else
         {
             MessageBox.Show("上传失败!");
         }
     }
     catch (Exception e)
     {
         MessageBox.Show("上传时出错:" + e.Message);
     }
 }
Example #3
0
 private void button4_Click(object sender, EventArgs e)
 {
     openFileDialog1.Title  = "添加更新文件...";
     openFileDialog1.Filter = "所有文件(*.*)|*.*";
     if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
     {
         FileInfo fi = new FileInfo(openFileDialog1.FileName);
         KellFileTransfer.FILELIST file = new KellFileTransfer.FILELIST();
         file.文件路径 = fi.Name;
         file.文件大小 = fi.Length;
         try
         {
             string fullname = this.dir + "\\" + file.文件路径;
             string newDir   = Path.GetDirectoryName(fullname);
             if (!Directory.Exists(newDir))
             {
                 Directory.CreateDirectory(newDir);
             }
             fi.CopyTo(fullname, true);
             list.Add(file);
             listBox1.Items.Add("文件路径:" + file.文件路径 + " | 文件大小:" + file.文件大小);
         }
         catch { }
     }
     openFileDialog1.Dispose();
 }
Example #4
0
        private void DownloadAttachments(List <Attachment> attachs)
        {
            selectedCount = attachs.Count;
            IPEndPoint ipep = KellFileTransfer.Common.GetDownloadIPEndPoint();

            KellFileTransfer.FileDownloadClient client = new KellFileTransfer.FileDownloadClient();
            client.DownloadFinishedSingle += new KellFileTransfer.DownloadSingleHandler(client_DownloadFinishedSingle);
            client.DownloadingError       += new KellFileTransfer.DownloadErrorHandler(client_DownloadingError);
            foreach (Attachment attach in attachs)
            {
                User user = attach.Uploader;
                //以下为禁止非原上传用户下载附件的代码,由于现在运行所有用户下载查看附件,所以屏蔽!
                //int index = attach.AttachmentFilename.IndexOf("\\");// 322\123.abc
                //if (index > 0 && user != null)
                //{
                //    string userId = attach.AttachmentFilename.Substring(0, index);
                //    int R;
                //    if (int.TryParse(userId, out R))
                //    {
                //        if (user.ID != R)
                //        {
                //            continue;//非法用户
                //        }
                //    }
                //}
                KellFileTransfer.FILELIST file = new KellFileTransfer.FILELIST();
                if (user != null)
                {
                    file.文件路径 = user.ID + "\\" + Path.GetFileName(attach.AttachmentFilename);
                }
                else
                {
                    file.文件路径 = Path.GetFileName(attach.AttachmentFilename);
                }
                file.文件大小 = attach.Size;
                bool f = client.DownloadFileFromServer(file, ipep.Address, ipep.Port);
                if (!f)
                {
                    MessageBox.Show("下载附件失败!很有可能连接到附件服务器的网络已断开。");
                }
            }
            button1.Refresh();
            //SolidBrush Backbrush = new SolidBrush(SystemColors.Control);
            //SolidBrush Forebrush = new SolidBrush(SystemColors.ControlText);
            //Rectangle rect = new Rectangle(0, 0, button1.Width, button1.Height);
            //using (Graphics g = button1.CreateGraphics())
            //{
            //    g.FillRectangle(Backbrush, rect);
            //    SizeF size = g.MeasureString("下载附件",button1.Font);
            //    PointF point = new PointF(button1.Width / 2 - size.Width / 2, button1.Height / 2 - size.Height / 2);
            //    g.DrawString("下载附件", button1.Font, Forebrush, point);
            //}
            //Backbrush.Dispose();
            //Forebrush.Dispose();
        }
Example #5
0
 public static void DownloadAttachment(bool open, User user, int attachId, string attachmentFileName, long size, IPEndPoint ipep = null)
 {
     if (string.IsNullOrEmpty(attachmentFileName))
     {
         return;
     }
     //以下为禁止非原上传用户下载附件的代码,由于现在运行所有用户下载查看附件,所以屏蔽!
     //int index = attachmentFileName.IndexOf("\\");// 322\123.abc
     //if (index > 0 && user != null)
     //{
     //    string userId = attachmentFileName.Substring(0, index);
     //    int R;
     //    if (int.TryParse(userId, out R))
     //    {
     //        if (user.ID != R)
     //        {
     //            return;//非法用户
     //        }
     //    }
     //}
     try
     {
         KellFileTransfer.FileDownloadClient download = new KellFileTransfer.FileDownloadClient();
         if (open)
         {
             download.DownloadFinishedSingle += new KellFileTransfer.DownloadSingleHandler(download_DownloadFinishedSingle);
         }
         KellFileTransfer.FILELIST attachment = new KellFileTransfer.FILELIST();
         attachment.文件路径 = attachmentFileName;
         attachment.文件大小 = size;
         if (ipep == null)
         {
             ipep = KellFileTransfer.Common.GetDownloadIPEndPoint();
         }
         if (download.DownloadFileFromServer(attachment, ipep.Address, ipep.Port))
         {
             AttachmentLogic.GetInstance().AfterDownload(attachId);
         }
         else
         {
             MessageBox.Show("下载附件失败!很有可能连接到附件服务器的网络已断开。");
         }
     }
     catch (Exception e)
     {
         MessageBox.Show("下载附件时出错:" + e.Message);
     }
 }
Example #6
0
 private void button5_Click(object sender, EventArgs e)
 {
     if (listBox1.SelectedIndex > -1)
     {
         int index = listBox1.SelectedIndex;
         KellFileTransfer.FILELIST file = list[listBox1.SelectedIndex] as KellFileTransfer.FILELIST;
         if (file != null)
         {
             try
             {
                 File.Delete(this.dir + "\\" + file.文件路径);
             }
             catch { }
             listBox1.Items.RemoveAt(index);
             list.RemoveAt(index);
         }
     }
 }
Example #7
0
 public static Attachment UploadAttachment(User user, string localFilename)
 {
     try
     {
         string dir = null;
         if (user != null)
         {
             dir = user.ID.ToString();
         }
         KellFileTransfer.FILELIST file = KellFileTransfer.Common.GetFILE(localFilename, dir);
         if (KellFileTransfer.FileUploader.SendFile(localFilename, KellFileTransfer.Common.GetUploadIPEndPoint(), user.ID.ToString()))
         {
             Attachment attch = new Attachment();
             attch.AttachmentFilename = file.文件路径;
             attch.Size     = file.文件大小;
             attch.Uploader = user;
             int    r = AttachmentLogic.GetInstance().AddAttachment(attch);
             string s = "";
             if (r > 0)
             {
                 attch.ID = r;
                 return(attch);
             }
             else
             {
                 s = "但保存到数据库失败!";
             }
             MessageBox.Show("上传成功!" + s);
         }
         else
         {
             MessageBox.Show("上传失败!");
         }
     }
     catch (Exception e)
     {
         MessageBox.Show("上传时出错:" + e.Message);
     }
     return(null);
 }
Example #8
0
 public static void DownloadAttachment(string attachmentFileName, long size, IPEndPoint ipep = null)
 {
     try
     {
         KellFileTransfer.FileDownloadClient download = new KellFileTransfer.FileDownloadClient();
         download.DownloadFinishedSingle += new KellFileTransfer.DownloadSingleHandler(download_DownloadFinishedSingle);
         KellFileTransfer.FILELIST attachment = new KellFileTransfer.FILELIST();
         attachment.文件路径 = attachmentFileName;
         attachment.文件大小 = size;
         if (ipep == null)
         {
             ipep = KellFileTransfer.Common.GetDownloadIPEndPoint();
         }
         if (!download.DownloadFileFromServer(attachment, ipep.Address, ipep.Port))
         {
             MessageBox.Show("下载失败!");
         }
     }
     catch (Exception e)
     {
         MessageBox.Show("下载时出错:" + e.Message);
     }
 }