4
* Copyright 2009, Moxiecode Systems AB
5
* Released under GPL License.
7
* License: http://www.plupload.com/license
8
* Contributing: http://www.plupload.com/contributing
13
using System.Windows.Controls;
14
using System.Windows.Documents;
15
using System.Windows.Ink;
16
using System.Windows.Input;
17
using System.Windows.Media;
18
using System.Windows.Media.Animation;
19
using System.Windows.Shapes;
20
using System.Windows.Browser;
23
using System.Collections.Generic;
24
using System.Threading;
25
using Moxiecode.Plupload;
27
namespace Moxiecode.Plupload {
29
/// Partial page class for the Silverlight page.
31
public partial class Page : UserControl {
32
#region private fields
33
private Dictionary<string, FileReference> files;
34
private int idCount = 0;
35
private FileReference currentFile;
36
private string id, filter;
37
private bool multiselect;
38
private bool disabled = false;
44
/// <param name="init_params">Silverlight init params.</param>
45
public Page(IDictionary<string, string> init_params) {
46
InitializeComponent();
48
HtmlPage.RegisterScriptableObject("Upload", this);
50
this.files = new Dictionary<string, FileReference>();
51
this.id = init_params["id"];
52
this.filter = init_params["filter"];
53
this.multiselect = Convert.ToBoolean(init_params["multiselect"]);
55
this.FireEvent("Init");
56
this.MouseLeftButtonUp += new MouseButtonEventHandler(OnClick);
58
this.MouseLeftButtonDown += new MouseButtonEventHandler(OnMouseLeftButtonDown);
59
this.MouseEnter += new MouseEventHandler(OnMouseEnter);
60
this.MouseLeave += new MouseEventHandler(OnMouseLeave);
63
private void OnClick(object sender, MouseEventArgs e) {
68
OpenFileDialog dlg = new OpenFileDialog();
70
this.FireEvent("StartSelectFiles");
73
dlg.Multiselect = this.multiselect;
74
dlg.Filter = this.filter;
76
if ((bool) dlg.ShowDialog()) {
77
foreach (FileInfo file in dlg.Files) {
78
FileReference uploadFile = new FileReference("u" + this.idCount++, file);
80
uploadFile.UploadChunkComplete += delegate(object up_sender, UploadEventArgs args) {
81
FileReference evtFile = (FileReference) up_sender;
83
this.FireEvent("UploadChunkSuccessful", evtFile.Id, args.Chunk, args.Chunks, args.Response);
86
uploadFile.UploadComplete += delegate(object up_sender, UploadEventArgs args) {
87
FileReference evtFile = (FileReference) up_sender;
89
this.FireEvent("UploadSuccessful", evtFile.Id, args.Response);
92
uploadFile.Error += delegate(object up_sender, ErrorEventArgs args) {
93
FileReference evtFile = (FileReference) up_sender;
95
this.FireEvent("UploadChunkError", evtFile.Id, args.Chunk, args.Chunks, args.Message);
98
uploadFile.Progress += delegate(object up_sender, ProgressEventArgs args) {
99
FileReference evtFile = (FileReference) up_sender;
101
this.FireEvent("UploadFileProgress", evtFile.Id, args.Loaded, args.Total);
104
this.FireEvent("SelectFile", uploadFile.Id, uploadFile.Name, uploadFile.Size);
105
this.files[uploadFile.Id] = uploadFile;
108
this.FireEvent("SelectSuccessful");
110
this.FireEvent("SelectCancelled");
111
} catch (Exception ex) {
112
this.FireEvent("SelectError", ex.Message);
117
private void OnMouseLeftButtonDown(object sender, MouseEventArgs e) {
118
this.FireEvent("MouseLeftButtonDown");
121
private void OnMouseEnter(object sender, MouseEventArgs e) {
122
this.FireEvent("MouseEnter");
125
private void OnMouseLeave(object sender, MouseEventArgs e) {
126
this.FireEvent("MouseLeave");
130
/// Reference to page level plupload.silverlight script object.
132
public ScriptObject PluploadScriptObject {
133
get { return ((ScriptObject) HtmlPage.Window.Eval("plupload.silverlight")); }
137
/// Fires a specific event to the page level multi upload script.
139
/// <param name="name">Event name to fire.</param>
140
public void FireEvent(string name) {
141
this.PluploadScriptObject.Invoke("trigger", new string[] { this.id, name });
145
/// Fires a specific event to the page level multi upload script.
147
/// <param name="name">Event name to fire.</param>
148
/// <param name="paramlist">Numerous parameters to send.</param>
149
public void FireEvent(string name, params object[] paramlist) {
150
List<object> args = new List<object>(paramlist);
152
args.Insert(0, name);
153
args.Insert(0, this.id);
155
this.PluploadScriptObject.Invoke("trigger", args.ToArray());
160
/// Uploads a specific file by id to the specific url and using a chunks.
162
/// <param name="id">File id to upload.</param>
163
/// <param name="upload_url">Url to upload to.</param>
164
/// <param name="chunk_size">Chunk size to use.</param>
165
public void UploadFile(string id, string upload_url, string json_settings) {
166
if (this.files.ContainsKey(id)) {
167
FileReference file = this.files[id];
169
this.currentFile = file;
170
file.Upload(upload_url, json_settings);
176
/// Removes the specified file by id.
178
/// <param name="id">File id to remove.</param>
179
public void RemoveFile(string id) {
180
if (this.files.ContainsKey(id))
181
this.files[id] = null;
186
/// Clears all files.
188
public void ClearFiles() {
189
this.files = new Dictionary<string, FileReference>();
194
/// Uploads the next chunk of the current file. Returns true/false if there is more chunks.
196
/// <return>true/false if there is more chunks</return>
197
public bool UploadNextChunk() {
198
if (this.currentFile != null)
199
return this.currentFile.UploadNextChunk();
208
public void CancelUpload() {
209
if (this.currentFile != null)
210
this.currentFile.CancelUpload();
215
/// Disable dialog trigger.
217
public void DisableBrowse(bool disabled = true)
219
this.disabled = disabled;
223
/// Send debug message to firebug console.
225
/// <param name="msg">Message to write.</param>
226
private void Debug(string msg) {
227
((ScriptObject) HtmlPage.Window.Eval("console")).Invoke("log", new string[] { msg });