コード例 #1
0
ファイル: FileUploader.cs プロジェクト: wenysky/dnt31-lite
        public FileUploader(UserFile file)
        {
            _file = file;

            _dataLength = _file.FileStream.Length;
            _dataSent = 0;

            _client = Utils.CreateServiceClient(); //new MixObjectsSoapClient("", "http://localhost/services/MixObjects.asmx");
            //事件绑定
            _client.StoreFileAdvancedCompleted += new EventHandler<StoreFileAdvancedCompletedEventArgs>(_client_StoreFileAdvancedCompleted);
            _client.CancelUploadCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(_client_CancelUploadCompleted);
            _client.ChannelFactory.Closed += new EventHandler(ChannelFactory_Closed);
        }
コード例 #2
0
ファイル: Page.xaml.cs プロジェクト: wenysky/dnt31-lite
        /// <summary>
        /// 选择文件对话框事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SelectFilesButton_Click(object sender, RoutedEventArgs e)
        {

            if (AttachmentList.Count >= _maxAttachments)
            {
                ShowMessageBox("\r\n您上传的文件数已达到系统规定的上限: " + _maxAttachments + ".");
                return;
            }

            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Multiselect = true;

            try
            {
                if(!string.IsNullOrEmpty(_fileFilter))
                    ofd.Filter = _fileFilter;
            }
            catch(ArgumentException ex)
            {
                ShowMessageBox("错误的文件过滤配置: " + ex.Message);
            }

            if (ofd.ShowDialog() == true)
            {
                if (filecount == 0)
                    filecount = AttachmentList.Count;

                foreach (FileInfo file in ofd.Files)
                {
                    if ((filecount + 1) > _maxAttachments)
                    {
                        ShowMessageBox("\r\n您上传的文件数已达到系统规定的上限: " + _maxAttachments + ".");
                        return;
                    }
                    filecount++;

                    string fileName = file.Name;
                    UserFile userFile = new UserFile();
                    userFile.FileName = file.Name;
                    userFile.FileStream = file.OpenRead();
                    userFile.ViewStream = file.OpenRead();

                    //总上传值在规定范围内时
                    if (_todayAttachSize < (_todayUploadSize + _wantUploadSize + userFile.FileStream.Length))
                    {
                        ShowMessageBox("\r\n当前附件大小: " + Math.Round((decimal)userFile.FileStream.Length / 1024 / 1024, 2) + " MB, 而今天还可以上传:" + Math.Round((decimal)(_todayAttachSize - _todayUploadSize) / 1024 / 1024, 2) + " MB.");
                        break;
                    }
                    //当单个文件大小大于最大上传附件尺寸时
                    if (userFile.FileStream.Length > _maxFileSize)
                    {
                        ShowMessageBox("\r\n当前附件大小: " + Math.Round((decimal)userFile.FileStream.Length / 1024 / 1024, 2) + " MB, 而单个附件允许最大尺寸为: " + Math.Round((decimal)_maxFileSize / 1024 / 1024, 2) + " MB.\r\n");
                        break;
                    }

                    //只对下面的图标文件类型生成缩略图
                    if ((".jpg.jpeg.png.bmp").IndexOf(Utils.CutString(userFile.FileName, userFile.FileName.LastIndexOf(".") + 1).ToLower()) >= 0)
                    {
                        BitmapImage bitimage = new BitmapImage();
                        try
                        {
                            bitimage.SetSource(userFile.ViewStream);
                            //ThumbnailImage.Source = bitimage;
                            //ThumbnailImage.Visibility = Visibility.Visible;
                            //Canvas.SetLeft(ThumbnailImageBorder, e.X + 50);
                            //Canvas.SetTop(ThumbnailImageBorder, e.Y - 30);
                            //object o = ThumbnailImage.GetValue(Canvas.ActualHeightProperty);
                            //ThumbnailImageBorder.Height = ThumbnailImage.Height + 10;
                            //expandImage.Begin();
                            _files.Add(userFile);
                            _wantUploadSize += userFile.FileStream.Length;  
                        }
                        catch 
                        {
                            ShowMessageBox("\r\n上传图片信息错误,请检查!");
                            break;
                        }
                    }
                    else
                    {
                        //向文件列表中添加文件信息
                        _files.Add(userFile);
                        _wantUploadSize += userFile.FileStream.Length;  
                    }
                    
                }
            }
        }