Beispiel #1
0
        void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            string path = e.Argument.ToString();

            System.IO.FileInfo fileInfoIO = new System.IO.FileInfo(path);
            // 要上传的文件地址
            FileStream fs = File.OpenRead(fileInfoIO.FullName);
            // 实例化服务客户的
            ServiceFileClient client = new ServiceFileClient();

            try
            {
                int maxSiz = 1024 * 100;
                // 根据文件名获取服务器上的文件
                CustomFileInfo file = client.GetFileInfo(fileInfoIO.Name);
                if (file == null)
                {
                    file        = new CustomFileInfo();
                    file.OffSet = 0;
                }
                file.Name   = fileInfoIO.Name;
                file.Length = fs.Length;
                if (file.Length > Int32.MaxValue)
                {
                    MessageBox.Show("上传文件大小不能超过2G");
                }
                else
                {
                    if (file.Length == file.OffSet) //如果文件的长度等于文件的偏移量,说明文件已经上传完成
                    {
                        MessageBox.Show("该文件已存在");
                        e.Result = false;   // 设置异步操作结果为false
                    }
                    else
                    {
                        while (file.Length != file.OffSet)
                        {
                            file.SendByte = new byte[file.Length - file.OffSet <= maxSiz ? file.Length - file.OffSet : maxSiz]; //设置传递的数据的大小

                            fs.Position = file.OffSet;                                                                          //设置本地文件数据的读取位置
                            fs.Read(file.SendByte, 0, file.SendByte.Length);                                                    //把数据写入到file.Data中
                            file = client.UpLoadFileInfo(file);                                                                 //上传
                            //int percent = (int)((double)file.OffSet / (double)((long)file.Length)) * 100;
                            int percent = (int)(((double)file.OffSet / (double)((long)file.Length)) * 100);
                            (sender as BackgroundWorker).ReportProgress(percent);
                        }
                        // 移动文件到临时目录(此部分创建可以使用sqlserver数据库代替)
                        string address = string.Format(@"{0}\{1}", Helper.ServerFolderPath, file.Name);
                        fileInfoIO.CopyTo(address, true);
                        LoadUpLoadFile();
                        e.Result = true;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                fs.Close();
                fs.Dispose();
                client.Close();
                client.Abort();
            }
        }
Beispiel #2
0
        void upLoad(object parame)
        {
            try
            {
                string path   = parame.ToString();
                int    maxSiz = 1024 * 100;

                System.IO.FileInfo fileInfoIO = new System.IO.FileInfo(path);
                // 要上传的文件地址
                FileStream fs = File.OpenRead(fileInfoIO.FullName);
                // 实例化服务客户的
                ServiceFileClient client = new ServiceFileClient();

                // 根据文件名获取服务器上的文件

                CustomFileInfo file = client.GetFileInfo(fileInfoIO.Name);

                if (file == null)
                {
                    file        = new CustomFileInfo();
                    file.OffSet = 0;
                }
                file.Name   = fileInfoIO.Name;
                file.Length = fs.Length;

                if (file.Length == file.OffSet) //如果文件的长度等于文件的偏移量,说明文件已经上传完成
                {
                    MessageBox.Show("该文件已存在");
                }
                else
                {
                    //MessageBox.Show("1");

                    while (file.Length != file.OffSet)
                    {
                        file.SendByte = new byte[file.Length - file.OffSet <= maxSiz ? file.Length - file.OffSet : maxSiz]; //设置传递的数据的大小

                        fs.Position = file.OffSet;                                                                          //设置本地文件数据的读取位置
                        fs.Read(file.SendByte, 0, file.SendByte.Length);                                                    //把数据写入到file.Data中
                        // MessageBox.Show("2");
                        file = client.UpLoadFileInfo(file);                                                                 //上传
                        //int percent = (int)((double)file.OffSet / (double)((long)file.Length)) * 100;
                        int percent = (int)(((double)file.OffSet / (double)((long)file.Length)) * 100);

                        if (progressBar.InvokeRequired)
                        {
                            // MessageBox.Show("3");
                            progressBar.Invoke((ThreadStart) delegate
                            {
                                progressBar.Value = percent;
                                progressBar.Update();
                            });
                        }
                        else
                        {
                            //MessageBox.Show("4");
                            progressBar.Value = percent;
                            progressBar.Update();
                        }
                    }
                    // 移动文件到临时目录(此部分创建可以使用sqlserver数据库代替)
                    string address = string.Format(@"{0}\{1}", Helper.ServerFolderPath, file.Name);

                    fileInfoIO.CopyTo(address, true);
                    LoadUpLoadFile();
                    MessageBox.Show("success");
                }
                fs.Close();
                client.Close();
                //if (this.btnUpload.InvokeRequired)
                //{
                //    this.btnUpload.Invoke((ThreadStart)delegate
                //    {
                //        this.btnUpload.Enabled = true;
                //    });
                //}
                //else
                //{
                this.btnUpload.Enabled = true;
                this.btnUpload.Update();
                //}
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }