26 SWFUpload.prototype.initSWFUpload =
function (settings) {
28 this.customSettings = {};
29 this.settings = settings;
31 this.movieName =
"SWFUpload_" +
SWFUpload.movieCount++;
32 this.movieElement = null;
36 SWFUpload.instances[this.movieName] =
this;
43 delete SWFUpload.instances[this.movieName];
55 QUEUE_LIMIT_EXCEEDED : -100,
56 FILE_EXCEEDS_SIZE_LIMIT : -110,
57 ZERO_BYTE_FILE : -120,
58 INVALID_FILETYPE : -130
62 MISSING_UPLOAD_URL : -210,
64 SECURITY_ERROR : -230,
65 UPLOAD_LIMIT_EXCEEDED : -240,
67 SPECIFIED_FILE_ID_NOT_FOUND : -260,
68 FILE_VALIDATION_FAILED : -270,
69 FILE_CANCELLED : -280,
90 TRANSPARENT :
"transparent",
97 if (typeof(url) !==
"string" || url.match(/^https?:\/\
101 var currentURL = window.location.protocol +
"//" + window.location.hostname + (window.location.port ?
":" + window.location.port :
"");
104 if (indexSlash <= 0) {
107 path = window.location.pathname.substr(0, indexSlash) +
"/";
122 this.ensureDefault =
function (settingName, defaultValue) {
123 this.settings[settingName] = (this.settings[settingName] == undefined) ? defaultValue : this.settings[settingName];
127 this.ensureDefault(
"upload_url",
"");
128 this.ensureDefault(
"preserve_relative_urls",
false);
129 this.ensureDefault(
"file_post_name",
"Filedata");
130 this.ensureDefault(
"post_params", {});
131 this.ensureDefault(
"use_query_string",
false);
132 this.ensureDefault(
"requeue_on_error",
false);
133 this.ensureDefault(
"http_success", []);
134 this.ensureDefault(
"assume_success_timeout", 0);
137 this.ensureDefault(
"file_types",
"*.*");
138 this.ensureDefault(
"file_types_description",
"All Files");
139 this.ensureDefault(
"file_size_limit", 0);
140 this.ensureDefault(
"file_upload_limit", 0);
141 this.ensureDefault(
"file_queue_limit", 0);
144 this.ensureDefault(
"flash_url",
"swfupload.swf");
145 this.ensureDefault(
"prevent_swf_caching",
true);
148 this.ensureDefault(
"button_image_url",
"");
149 this.ensureDefault(
"button_width", 1);
150 this.ensureDefault(
"button_height", 1);
151 this.ensureDefault(
"button_text",
"");
152 this.ensureDefault(
"button_text_style",
"color: #000000; font-size: 16pt;");
153 this.ensureDefault(
"button_text_top_padding", 0);
154 this.ensureDefault(
"button_text_left_padding", 0);
155 this.ensureDefault(
"button_action",
SWFUpload.BUTTON_ACTION.SELECT_FILES);
156 this.ensureDefault(
"button_disabled",
false);
157 this.ensureDefault(
"button_placeholder_id",
"");
158 this.ensureDefault(
"button_placeholder", null);
159 this.ensureDefault(
"button_cursor",
SWFUpload.CURSOR.ARROW);
160 this.ensureDefault(
"button_window_mode",
SWFUpload.WINDOW_MODE.WINDOW);
163 this.ensureDefault(
"debug",
false);
164 this.settings.debug_enabled = this.settings.debug;
168 this.ensureDefault(
"swfupload_loaded_handler", null);
169 this.ensureDefault(
"file_dialog_start_handler", null);
170 this.ensureDefault(
"file_queued_handler", null);
171 this.ensureDefault(
"file_queue_error_handler", null);
172 this.ensureDefault(
"file_dialog_complete_handler", null);
174 this.ensureDefault(
"upload_start_handler", null);
175 this.ensureDefault(
"upload_progress_handler", null);
176 this.ensureDefault(
"upload_error_handler", null);
177 this.ensureDefault(
"upload_success_handler", null);
178 this.ensureDefault(
"upload_complete_handler", null);
182 this.ensureDefault(
"custom_settings", {});
185 this.customSettings = this.settings.custom_settings;
188 if (!!this.settings.prevent_swf_caching) {
189 this.settings.flash_url = this.settings.flash_url + (this.settings.flash_url.indexOf(
"?") < 0 ?
"?" :
"&") +
"preventswfcaching=" +
new Date().getTime();
192 if (!this.settings.preserve_relative_urls) {
194 this.settings.upload_url =
SWFUpload.completeURL(this.settings.upload_url);
195 this.settings.button_image_url =
SWFUpload.completeURL(this.settings.button_image_url);
198 delete this.ensureDefault;
203 var targetElement, tempParent;
206 if (document.getElementById(
this.movieName) !== null) {
207 throw "ID " + this.movieName +
" is already in use. The Flash Object could not be added";
211 targetElement = document.getElementById(this.settings.button_placeholder_id) || this.settings.button_placeholder;
213 if (targetElement == undefined) {
214 throw "Could not find the placeholder element: " + this.settings.button_placeholder_id;
218 tempParent = document.createElement(
"div");
220 targetElement.parentNode.replaceChild(tempParent.firstChild, targetElement);
223 if (window[this.movieName] == undefined) {
232 return [
'<object id="', this.movieName,
'" type="application/x-shockwave-flash" data="', this.settings.flash_url,
'" width="', this.settings.button_width,
'" height="', this.settings.button_height,
'" class="swfupload">',
233 '<param name="wmode" value="', this.settings.button_window_mode,
'" />',
234 '<param name="movie" value="', this.settings.flash_url,
'" />',
235 '<param name="quality" value="high" />',
236 '<param name="menu" value="false" />',
237 '<param name="allowScriptAccess" value="always" />',
238 '<param name="flashvars" value="' + this.
getFlashVars() +
'" />',
239 '</object>'].join(
"");
247 var httpSuccessString = this.settings.http_success.join(
",");
250 return [
"movieName=", encodeURIComponent(this.movieName),
251 "&uploadURL=", encodeURIComponent(this.settings.upload_url),
252 "&useQueryString=", encodeURIComponent(this.settings.use_query_string),
253 "&requeueOnError=", encodeURIComponent(this.settings.requeue_on_error),
254 "&httpSuccess=", encodeURIComponent(httpSuccessString),
255 "&assumeSuccessTimeout=", encodeURIComponent(this.settings.assume_success_timeout),
256 "&params=", encodeURIComponent(paramString),
257 "&filePostName=", encodeURIComponent(this.settings.file_post_name),
258 "&fileTypes=", encodeURIComponent(this.settings.file_types),
259 "&fileTypesDescription=", encodeURIComponent(this.settings.file_types_description),
260 "&fileSizeLimit=", encodeURIComponent(this.settings.file_size_limit),
261 "&fileUploadLimit=", encodeURIComponent(this.settings.file_upload_limit),
262 "&fileQueueLimit=", encodeURIComponent(this.settings.file_queue_limit),
263 "&debugEnabled=", encodeURIComponent(this.settings.debug_enabled),
264 "&buttonImageURL=", encodeURIComponent(this.settings.button_image_url),
265 "&buttonWidth=", encodeURIComponent(this.settings.button_width),
266 "&buttonHeight=", encodeURIComponent(this.settings.button_height),
267 "&buttonText=", encodeURIComponent(this.settings.button_text),
268 "&buttonTextTopPadding=", encodeURIComponent(this.settings.button_text_top_padding),
269 "&buttonTextLeftPadding=", encodeURIComponent(this.settings.button_text_left_padding),
270 "&buttonTextStyle=", encodeURIComponent(this.settings.button_text_style),
271 "&buttonAction=", encodeURIComponent(this.settings.button_action),
272 "&buttonDisabled=", encodeURIComponent(this.settings.button_disabled),
273 "&buttonCursor=", encodeURIComponent(this.settings.button_cursor)
280 if (this.movieElement == undefined) {
281 this.movieElement = document.getElementById(this.movieName);
284 if (this.movieElement === null) {
285 throw "Could not find Flash element";
288 return this.movieElement;
294 var postParams = this.settings.post_params;
295 var paramStringPairs = [];
297 if (typeof(postParams) ===
"object") {
298 for (var name in postParams) {
299 if (postParams.hasOwnProperty(name)) {
300 paramStringPairs.push(encodeURIComponent(name.toString()) +
"=" + encodeURIComponent(postParams[name].toString()));
305 return paramStringPairs.join(
"&");
319 var movieElement = null;
322 if (movieElement && typeof(movieElement.CallFunction) ===
"unknown") {
324 for (var i in movieElement) {
326 if (typeof(movieElement[i]) ===
"function") {
327 movieElement[i] = null;
334 movieElement.parentNode.removeChild(movieElement);
339 window[this.movieName] = null;
342 SWFUpload.instances[this.movieName] = null;
343 delete SWFUpload.instances[this.movieName];
345 this.movieElement = null;
346 this.settings = null;
347 this.customSettings = null;
348 this.eventQueue = null;
349 this.movieName = null;
366 "---SWFUpload Instance Info---\n",
368 "Movie Name: ",
this.movieName,
"\n",
370 "\t",
"upload_url: ",
this.settings.upload_url,
"\n",
371 "\t",
"flash_url: ",
this.settings.flash_url,
"\n",
372 "\t",
"use_query_string: ",
this.settings.use_query_string.toString(),
"\n",
373 "\t",
"requeue_on_error: ", this.settings.requeue_on_error.toString(),
"\n",
374 "\t",
"http_success: ", this.settings.http_success.join(
", "),
"\n",
375 "\t",
"assume_success_timeout: ", this.settings.assume_success_timeout,
"\n",
376 "\t",
"file_post_name: ", this.settings.file_post_name,
"\n",
377 "\t",
"post_params: ", this.settings.post_params.toString(),
"\n",
378 "\t",
"file_types: ", this.settings.file_types,
"\n",
379 "\t",
"file_types_description: ", this.settings.file_types_description,
"\n",
380 "\t",
"file_size_limit: ", this.settings.file_size_limit,
"\n",
381 "\t",
"file_upload_limit: ", this.settings.file_upload_limit,
"\n",
382 "\t",
"file_queue_limit: ", this.settings.file_queue_limit,
"\n",
383 "\t",
"debug: ", this.settings.debug.toString(),
"\n",
385 "\t",
"prevent_swf_caching: ", this.settings.prevent_swf_caching.toString(),
"\n",
387 "\t",
"button_placeholder_id: ", this.settings.button_placeholder_id.toString(),
"\n",
388 "\t",
"button_placeholder: ", (this.settings.button_placeholder ?
"Set" :
"Not Set"),
"\n",
389 "\t",
"button_image_url: ", this.settings.button_image_url.toString(),
"\n",
390 "\t",
"button_width: ", this.settings.button_width.toString(),
"\n",
391 "\t",
"button_height: ", this.settings.button_height.toString(),
"\n",
392 "\t",
"button_text: ", this.settings.button_text.toString(),
"\n",
393 "\t",
"button_text_style: ", this.settings.button_text_style.toString(),
"\n",
394 "\t",
"button_text_top_padding: ", this.settings.button_text_top_padding.toString(),
"\n",
395 "\t",
"button_text_left_padding: ", this.settings.button_text_left_padding.toString(),
"\n",
396 "\t",
"button_action: ", this.settings.button_action.toString(),
"\n",
397 "\t",
"button_disabled: ", this.settings.button_disabled.toString(),
"\n",
399 "\t",
"custom_settings: ", this.settings.custom_settings.toString(),
"\n",
401 "\t",
"swfupload_loaded_handler assigned: ", (typeof this.settings.swfupload_loaded_handler ===
"function").toString(),
"\n",
402 "\t",
"file_dialog_start_handler assigned: ", (typeof this.settings.file_dialog_start_handler ===
"function").toString(),
"\n",
403 "\t",
"file_queued_handler assigned: ", (typeof this.settings.file_queued_handler ===
"function").toString(),
"\n",
404 "\t",
"file_queue_error_handler assigned: ", (typeof this.settings.file_queue_error_handler ===
"function").toString(),
"\n",
405 "\t",
"upload_start_handler assigned: ", (typeof this.settings.upload_start_handler ===
"function").toString(),
"\n",
406 "\t",
"upload_progress_handler assigned: ", (typeof this.settings.upload_progress_handler ===
"function").toString(),
"\n",
407 "\t",
"upload_error_handler assigned: ", (typeof this.settings.upload_error_handler ===
"function").toString(),
"\n",
408 "\t",
"upload_success_handler assigned: ", (typeof this.settings.upload_success_handler ===
"function").toString(),
"\n",
409 "\t",
"upload_complete_handler assigned: ", (typeof this.settings.upload_complete_handler ===
"function").toString(),
"\n",
410 "\t",
"debug_handler assigned: ", (typeof this.settings.debug_handler ===
"function").toString(),
"\n"
419 SWFUpload.prototype.addSetting =
function (name, value, default_value) {
420 if (value == undefined) {
421 return (this.settings[name] = default_value);
423 return (this.settings[name] = value);
429 if (this.settings[name] != undefined) {
430 return this.settings[name];
441 SWFUpload.prototype.callFlash =
function (functionName, argumentArray) {
442 argumentArray = argumentArray || [];
445 var returnValue, returnString;
449 returnString = movieElement.CallFunction(
'<invoke name="' + functionName +
'" returntype="javascript">' + __flash__argumentsToXML(argumentArray, 0) +
'</invoke>');
450 returnValue = eval(returnString);
452 throw "Call to " + functionName +
" failed";
456 if (returnValue != undefined && typeof returnValue.post ===
"object") {
496 SWFUpload.prototype.cancelUpload =
function (fileID, triggerErrorEvent) {
497 if (triggerErrorEvent !==
false) {
498 triggerErrorEvent =
true;
500 this.
callFlash(
"CancelUpload", [fileID, triggerErrorEvent]);
527 this.
callFlash(
"SetStats", [statsObject]);
533 if (typeof(fileID) ===
"number") {
534 return this.
callFlash(
"GetFileByIndex", [fileID]);
536 return this.
callFlash(
"GetFile", [fileID]);
543 SWFUpload.prototype.addFileParam =
function (fileID, name, value) {
544 return this.
callFlash(
"AddFileParam", [fileID, name, value]);
549 SWFUpload.prototype.removeFileParam =
function (fileID, name) {
550 this.
callFlash(
"RemoveFileParam", [fileID, name]);
555 this.settings.upload_url = url.toString();
560 SWFUpload.prototype.setPostParams =
function (paramsObject) {
561 this.settings.post_params = paramsObject;
562 this.
callFlash(
"SetPostParams", [paramsObject]);
566 SWFUpload.prototype.addPostParam =
function (name, value) {
567 this.settings.post_params[name] = value;
568 this.
callFlash(
"SetPostParams", [this.settings.post_params]);
573 delete this.settings.post_params[name];
574 this.
callFlash(
"SetPostParams", [this.settings.post_params]);
578 SWFUpload.prototype.setFileTypes =
function (types, description) {
579 this.settings.file_types = types;
580 this.settings.file_types_description = description;
581 this.
callFlash(
"SetFileTypes", [types, description]);
585 SWFUpload.prototype.setFileSizeLimit =
function (fileSizeLimit) {
586 this.settings.file_size_limit = fileSizeLimit;
587 this.
callFlash(
"SetFileSizeLimit", [fileSizeLimit]);
591 SWFUpload.prototype.setFileUploadLimit =
function (fileUploadLimit) {
592 this.settings.file_upload_limit = fileUploadLimit;
593 this.
callFlash(
"SetFileUploadLimit", [fileUploadLimit]);
597 SWFUpload.prototype.setFileQueueLimit =
function (fileQueueLimit) {
598 this.settings.file_queue_limit = fileQueueLimit;
599 this.
callFlash(
"SetFileQueueLimit", [fileQueueLimit]);
603 SWFUpload.prototype.setFilePostName =
function (filePostName) {
604 this.settings.file_post_name = filePostName;
605 this.
callFlash(
"SetFilePostName", [filePostName]);
609 SWFUpload.prototype.setUseQueryString =
function (useQueryString) {
610 this.settings.use_query_string = useQueryString;
611 this.
callFlash(
"SetUseQueryString", [useQueryString]);
615 SWFUpload.prototype.setRequeueOnError =
function (requeueOnError) {
616 this.settings.requeue_on_error = requeueOnError;
617 this.
callFlash(
"SetRequeueOnError", [requeueOnError]);
621 SWFUpload.prototype.setHTTPSuccess =
function (http_status_codes) {
622 if (typeof http_status_codes ===
"string") {
623 http_status_codes = http_status_codes.replace(
" ",
"").split(
",");
626 this.settings.http_success = http_status_codes;
627 this.
callFlash(
"SetHTTPSuccess", [http_status_codes]);
631 SWFUpload.prototype.setAssumeSuccessTimeout =
function (timeout_seconds) {
632 this.settings.assume_success_timeout = timeout_seconds;
633 this.
callFlash(
"SetAssumeSuccessTimeout", [timeout_seconds]);
637 SWFUpload.prototype.setDebugEnabled =
function (debugEnabled) {
638 this.settings.debug_enabled = debugEnabled;
639 this.
callFlash(
"SetDebugEnabled", [debugEnabled]);
643 SWFUpload.prototype.setButtonImageURL =
function (buttonImageURL) {
644 if (buttonImageURL == undefined) {
648 this.settings.button_image_url = buttonImageURL;
649 this.
callFlash(
"SetButtonImageURL", [buttonImageURL]);
653 SWFUpload.prototype.setButtonDimensions =
function (width, height) {
654 this.settings.button_width = width;
655 this.settings.button_height = height;
658 if (movie != undefined) {
659 movie.style.width = width +
"px";
660 movie.style.height = height +
"px";
663 this.
callFlash(
"SetButtonDimensions", [width, height]);
667 this.settings.button_text = html;
671 SWFUpload.prototype.setButtonTextPadding =
function (left, top) {
672 this.settings.button_text_top_padding = top;
673 this.settings.button_text_left_padding = left;
674 this.
callFlash(
"SetButtonTextPadding", [left, top]);
678 SWFUpload.prototype.setButtonTextStyle =
function (css) {
679 this.settings.button_text_style = css;
680 this.
callFlash(
"SetButtonTextStyle", [css]);
683 SWFUpload.prototype.setButtonDisabled =
function (isDisabled) {
684 this.settings.button_disabled = isDisabled;
685 this.
callFlash(
"SetButtonDisabled", [isDisabled]);
688 SWFUpload.prototype.setButtonAction =
function (buttonAction) {
689 this.settings.button_action = buttonAction;
690 this.
callFlash(
"SetButtonAction", [buttonAction]);
694 SWFUpload.prototype.setButtonCursor =
function (cursor) {
695 this.settings.button_cursor = cursor;
696 this.
callFlash(
"SetButtonCursor", [cursor]);
712 SWFUpload.prototype.queueEvent =
function (handlerName, argumentArray) {
715 if (argumentArray == undefined) {
717 }
else if (!(argumentArray instanceof Array)) {
718 argumentArray = [argumentArray];
722 if (typeof this.settings[handlerName] ===
"function") {
724 this.eventQueue.push(
function () {
725 this.settings[handlerName].apply(
this, argumentArray);
729 setTimeout(
function () {
730 self.executeNextEvent();
733 }
else if (this.settings[handlerName] !== null) {
734 throw "Event handler " + handlerName +
" is unknown or is not a function";
743 var f = this.eventQueue ? this.eventQueue.shift() : null;
744 if (typeof(f) ===
"function") {
752 SWFUpload.prototype.unescapeFilePostParams =
function (file) {
753 var reg = /[$]([0-9
a-f]{4})/i;
754 var unescapedPost = {};
757 if (file != undefined) {
758 for (var k in file.post) {
759 if (file.post.hasOwnProperty(k)) {
762 while ((match = reg.exec(uk)) !== null) {
763 uk = uk.replace(match[0], String.fromCharCode(parseInt(
"0x" + match[1], 16)));
765 unescapedPost[uk] = file.post[k];
769 file.post = unescapedPost;
776 SWFUpload.prototype.testExternalInterface =
function () {
778 return this.
callFlash(
"TestExternalInterface");
791 this.
debug(
"Flash called back ready but the flash movie can't be found.");
805 if (this.movieElement && typeof(movieElement.CallFunction) ===
"unknown") {
806 this.
debug(
"Removing Flash functions hooks (this should only run in IE and should prevent memory leaks)");
807 for (var key in movieElement) {
809 if (typeof(movieElement[key]) ===
"function") {
810 movieElement[key] = null;
822 window[
"__flash__removeCallback"] =
function (instance, name) {
825 instance[name] = null;
849 SWFUpload.prototype.fileQueueError =
function (file, errorCode, message) {
851 this.
queueEvent(
"file_queue_error_handler", [file, errorCode, message]);
856 SWFUpload.prototype.fileDialogComplete =
function (numFilesSelected, numFilesQueued, numFilesInQueue) {
857 this.
queueEvent(
"file_dialog_complete_handler", [numFilesSelected, numFilesQueued, numFilesInQueue]);
862 this.
queueEvent(
"return_upload_start_handler", file);
865 SWFUpload.prototype.returnUploadStart =
function (file) {
867 if (typeof this.settings.upload_start_handler ===
"function") {
869 returnValue = this.settings.upload_start_handler.call(
this, file);
870 }
else if (this.settings.upload_start_handler != undefined) {
871 throw "upload_start_handler must be a function";
876 if (returnValue === undefined) {
880 returnValue = !!returnValue;
882 this.
callFlash(
"ReturnUploadStart", [returnValue]);
887 SWFUpload.prototype.uploadProgress =
function (file, bytesComplete, bytesTotal) {
889 this.
queueEvent(
"upload_progress_handler", [file, bytesComplete, bytesTotal]);
892 SWFUpload.prototype.uploadError =
function (file, errorCode, message) {
894 this.
queueEvent(
"upload_error_handler", [file, errorCode, message]);
897 SWFUpload.prototype.uploadSuccess =
function (file, serverData, responseReceived) {
899 this.
queueEvent(
"upload_success_handler", [file, serverData, responseReceived]);
904 this.
queueEvent(
"upload_complete_handler", file);
931 if (this.settings.debug) {
932 var exceptionMessage, exceptionValues = [];
935 if (typeof message ===
"object" && typeof message.name ===
"string" && typeof message.message ===
"string") {
936 for (var key in message) {
937 if (message.hasOwnProperty(key)) {
938 exceptionValues.push(key +
": " + message[key]);
941 exceptionMessage = exceptionValues.join(
"\n") ||
"";
942 exceptionValues = exceptionMessage.split(
"\n");
943 exceptionMessage =
"EXCEPTION: " + exceptionValues.join(
"\nEXCEPTION: ");
944 SWFUpload.Console.writeLine(exceptionMessage);
953 var console, documentForm;
956 console = document.getElementById(
"SWFUpload_Console");
959 documentForm = document.createElement(
"form");
960 document.getElementsByTagName(
"body")[0].appendChild(documentForm);
962 console = document.createElement(
"textarea");
963 console.id =
"SWFUpload_Console";
964 console.style.fontFamily =
"monospace";
965 console.setAttribute(
"wrap",
"off");
966 console.wrap =
"off";
967 console.style.overflow =
"auto";
968 console.style.width =
"700px";
969 console.style.height =
"350px";
970 console.style.margin =
"5px";
971 documentForm.appendChild(console);
974 console.value += message +
"\n";
976 console.scrollTop = console.scrollHeight - console.clientHeight;
978 alert(
"Exception: " + ex.name +
" Message: " + ex.message);