Example #1
0
        void UploadFiles()
        {
            while (m_files.Count(f => f.Status == UploadStatus.Uploading) < MaxConcurrentUploads && Uploading)
            {
                if (m_files.Count(f => f.Status != UploadStatus.Complete && f.Status != UploadStatus.Uploading && f.Status != UploadStatus.Error) > 0)
                {
                    UploadClient fu = m_files.FirstOrDefault(f => f.Status != UploadStatus.Complete && f.Status != UploadStatus.Uploading);

                    if (fu != null)
                    {
                        if (this.EachFilePreStartUpload != null)
                        {
                            this.EachFilePreStartUpload(this, new UploadPreStartEventArgs(fu.File.Name, fu.File));
                        }

                        fu.Upload();
                    }
                }
                else if (m_files.Count(f => f.Status == UploadStatus.Uploading) == 0)
                {
                    Uploading = false;
                    m_uploadFilesButton.Content = ResUploader.Uploader_UploadFiles;
                }
                else
                {
                    break;
                }
            }
        }
Example #2
0
        void upload_StatusChanged(object sender, EventArgs e)
        {
            UploadClient fu = sender as UploadClient;

            if (fu.Status == UploadStatus.Complete)
            {
                if (Uploading)
                {
                    UploadFiles();
                }
            }
            else if (fu.Status == UploadStatus.Removed)
            {
                m_files.Remove(fu);
                m_count--;
                if (m_files.Count == 0)
                {
                    this.m_borderBackground.Visibility = System.Windows.Visibility.Visible;
                }
                if (Uploading)
                {
                    UploadFiles();
                }
            }
            else if (fu.Status == UploadStatus.Uploading && m_files.Count(f => f.Status == UploadStatus.Uploading) < MaxConcurrentUploads)
            {
                UploadFiles();
            }
        }
Example #3
0
        void FileUploadRowControl_Loaded(object sender, RoutedEventArgs e)
        {
            UploadClient fu = DataContext as UploadClient;

            fu.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(fu_PropertyChanged);

            LoadImage(fu);
        }
Example #4
0
        void m_removeBtn_Click(object sender, RoutedEventArgs e)
        {
            UploadClient fu = DataContext as UploadClient;

            if (fu != null)
            {
                fu.RemoveUpload();
            }
        }
Example #5
0
        void fu_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            UploadClient fu = sender as UploadClient;

            if (e.PropertyName == "DisplayThumbnail")
            {
                LoadImage(fu);
            }

            else if (e.PropertyName == "Status")
            {
                switch (fu.Status)
                {
                case UploadStatus.Pending:
                    VisualStateManager.GoToState(this, "Pending", true);
                    break;

                case UploadStatus.Uploading:
                    VisualStateManager.GoToState(this, "Uploading", true);
                    break;

                case UploadStatus.Complete:
                    VisualStateManager.GoToState(this, "Complete", true);
                    break;

                case UploadStatus.Error:
                    VisualStateManager.GoToState(this, "Error", true);
                    break;

                case UploadStatus.Canceled:
                    VisualStateManager.GoToState(this, "Pending", true);
                    break;

                case UploadStatus.Canceling:
                    VisualStateManager.GoToState(this, "Pending", true);
                    break;

                case UploadStatus.Removed:
                    VisualStateManager.GoToState(this, "Pending", true);
                    break;

                default:
                    break;
                }
            }
        }
Example #6
0
        private void LoadImage(UploadClient fu)
        {
            if (fu != null && fu.DisplayThumbnail)
            {
                try
                {
                    if ((fu.Name.ToLower().EndsWith("jpg") || fu.Name.ToLower().EndsWith("jpeg") || fu.Name.ToLower().EndsWith("png")))
                    {
                        BitmapImage imageSource = new BitmapImage();

                        using (var stream = fu.File.OpenRead())
                        {
                            imageSource.SetSource(stream);
                        }
                        m_previewImage.Source     = imageSource;
                        m_previewImage.Visibility = Visibility.Visible;
                    }
                    else if (fu.Name.ToLower().EndsWith("gif"))
                    {
                        GifImage gif = null;
                        using (var stream = fu.File.OpenRead())
                        {
                            gif = GIFDecoder.Decode(stream);
                            if (gif.Frames.Count > 0)
                            {
                                m_previewImage.Source     = gif.Frames[0].Image;
                                m_previewImage.Visibility = Visibility.Visible;
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    string message = e.Message;
                }
            }
            else
            {
                Dispatcher.BeginInvoke(() =>
                {
                    m_previewImage.Visibility = Visibility.Collapsed;
                });
            }
        }
Example #7
0
        void m_addFilesButton_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();

            dlg.Filter      = Filter;
            dlg.Multiselect = MultiSelect;
            if (!MultiUpload)
            {
                dlg.Multiselect = false;
            }

            if ((bool)dlg.ShowDialog())
            {
                if (!MultiUpload)
                {
                    m_files.Clear();
                }

                if (CheckUploadFilesEvent != null)
                {
                    CheckUploadFileEventArgs checkArgs = new CheckUploadFileEventArgs(new List <FileInfo>(dlg.Files));
                    CheckUploadFilesEvent(this, checkArgs);
                    if (checkArgs.CheckResult == false)
                    {
                        return;
                    }
                }

                BitmapSource imageSource = null;

                foreach (FileInfo file in dlg.Files)
                {
                    string fileName = file.Name.ToLower().Trim();
                    var    suffix   = file.Extension;
                    //如果文件没有后缀名,需要提示User
                    if (string.IsNullOrEmpty(suffix))
                    {
                        this.ShowMessage(ResUploader.Uploader_InvalidFileExtension, Newegg.Oversea.Silverlight.Controls.Components.MessageType.Warning);
                        continue;
                    }

                    if (!this.Filter.Contains("*.*") && !this.Filter.ToLower().Contains(suffix.ToLower()))
                    {
                        this.ShowMessage(ResUploader.Uploader_InvalidFileExtension, Newegg.Oversea.Silverlight.Controls.Components.MessageType.Warning);
                        continue;
                    }
                    try
                    {
                        using (file.OpenRead())
                        {
                            if (fileName.EndsWith("jpg") || fileName.EndsWith("jpeg") || fileName.EndsWith("png"))
                            {
                                imageSource = new BitmapImage();
                                using (var stream = file.OpenRead())
                                {
                                    try
                                    {
                                        imageSource.SetSource(stream);
                                    }
                                    catch
                                    {
                                        //捕获特定异常,比如把a.xls改成a.jpg,此时需要给一个友好提示
                                        this.ShowMessage(ResUploader.Uploader_InvalidImageType, Newegg.Oversea.Silverlight.Controls.Components.MessageType.Warning);
                                        continue;
                                    }
                                }
                            }
                            else if (fileName.EndsWith("gif"))
                            {
                                GifImage gif = null;
                                using (var stream = file.OpenRead())
                                {
                                    try
                                    {
                                        gif = GIFDecoder.Decode(stream);
                                    }
                                    catch
                                    {
                                        //捕获特定异常,比如把a.jpg改成a.gif,此时需要给一个友好提示
                                        this.ShowMessage(ResUploader.Uploader_InvalidImageType, Newegg.Oversea.Silverlight.Controls.Components.MessageType.Warning);
                                        continue;
                                    }
                                    if (gif.Frames.Count > 0)
                                    {
                                        imageSource = gif.Frames[0].Image;
                                    }//如果user把jpg文件的后缀修改为.gif,需要抛异常提示User
                                    else
                                    {
                                        this.ShowMessage(ResUploader.Uploader_InvalidImageType, Newegg.Oversea.Silverlight.Controls.Components.MessageType.Warning);
                                        continue;
                                    }
                                }
                            }
                        }
                    }//检查文件是否正在使用
                    catch (IOException)
                    {
                        this.ShowMessage(ResUploader.Uploader_FileInUsed, Newegg.Oversea.Silverlight.Controls.Components.MessageType.Error);
                        continue;
                    }
                    //Catch other exception
                    catch (Exception ex)
                    {
                        this.ShowMessage(ex.Message, Newegg.Oversea.Silverlight.Controls.Components.MessageType.Error);
                        continue;
                    }

                    //check image width and height

                    if (this.DimensionsMatch != DimensionsMatchType.None)
                    {
                        if (fileName.EndsWith("jpg") || fileName.EndsWith("png") || fileName.EndsWith("gif") ||
                            fileName.EndsWith("jpeg") || fileName.EndsWith("bmp"))
                        {
                            try
                            {
                                if (imageSource != null)
                                {
                                    CheckImageDimensions(imageSource, file);
                                }
                            }
                            // Catch check exception
                            catch (ArgumentException ex)
                            {
                                this.ShowMessage(ex.Message, Newegg.Oversea.Silverlight.Controls.Components.MessageType.Warning);
                                continue;
                            }
                        }
                    }

                    UploadClient upload = new UploadClient(this.Dispatcher, UploadUrl, file, this);
                    upload.UploadCanceled += (o, b) =>
                    {
                        if (this.UploadCanceled != null)
                        {
                            UploadCanceled(this, b);
                        }
                    };

                    if (UploadChunkSize > 0)
                    {
                        upload.ChunkSize = UploadChunkSize;
                    }
                    if (MaximumFileNameLength > 0 && upload.File.Name.Length > MaximumFileNameLength)
                    {
                        this.ShowMessage(string.Format(ResUploader.Uploader_GreaterThanMaximumFileNameLength, upload.Name, upload.File.Name.Length, this.MaximumFileNameLength), Newegg.Oversea.Silverlight.Controls.Components.MessageType.Warning);
                        continue;
                    }

                    //单个文件超过限制的大小
                    if (MaximumUploadBytes >= 0 && upload.FileLength > MaximumUploadBytes)
                    {
                        this.ShowMessage(string.Format(ResUploader.Uploader_OutOfSingleAvailableUploadSize, upload.Name, this.MaximumUploadSize), Newegg.Oversea.Silverlight.Controls.Components.MessageType.Warning);
                        continue;
                    }

                    //全部的文件Size超过限制大小
                    if (MaximumTotalUploadBytes >= 0 && TotalUploadSize + upload.FileLength > MaximumTotalUploadBytes)
                    {
                        this.ShowMessage(string.Format(ResUploader.Uploader_OutOfTotalAvailableUploadSize, this.MaximumTotalUploadSize), Newegg.Oversea.Silverlight.Controls.Components.MessageType.Warning);
                        break;
                    }

                    //多文件上传的时候才作此Check
                    if (MaxNumberToUpload > -1 && MultiUpload)
                    {
                        if (++m_count > MaxNumberToUpload)
                        {
                            this.ShowMessage(string.Format(ResUploader.Uploader_OutOfMaximumNumber, this.MaxNumberToUpload), Newegg.Oversea.Silverlight.Controls.Components.MessageType.Warning);
                            break;
                        }
                    }
                    upload.DisplayThumbnail = (bool)this.m_displayThumbailCheckBox.IsChecked;
                    if (!this.AllowThumbnail)
                    {
                        upload.DisplayThumbnail = false;
                    }
                    upload.StatusChanged         += new EventHandler(upload_StatusChanged);
                    upload.UploadProgressChanged += new ProgressChangedEvent(upload_UploadProgressChanged);
                    upload.UploadCompleted       += new UploadCompletedEvent(upload_UploadCompleted);
                    m_files.Add(upload);
                    this.m_borderBackground.Visibility = System.Windows.Visibility.Collapsed;
                }
            }
        }