📄 swfupload.js
字号:
/**
* SWFUpload v2.0 by Jacob Roberts, Nov 2007, http://www.swfupload.org, http://linebyline.blogspot.com
* -------- -------- -------- -------- -------- -------- -------- --------
* SWFUpload is (c) 2006 Lars Huring and Mammon Media and is released under the MIT License:
* http://www.opensource.org/licenses/mit-license.php
*
* See Changelog.txt for version history
*
* Development Notes:
* * This version of SWFUpload requires Flash Player 9.0.28 and should autodetect the correct flash version.
* * In Linux Flash Player 9 setting the post file variable name does not work. It is always set to "Filedata".
* * There is a lot of repeated code that could be refactored to single functions. Feel free.
* * It's dangerous to do "circular calls" between Flash and JavaScript. I've taken steps to try to work around issues
* by having the event calls pipe through setTimeout. However you should still avoid calling in to Flash from
* within the event handler methods. Especially the "startUpload" event since it cannot use the setTimeout hack.
*/
/* *********** */
/* Constructor */
/* *********** */
var SWFUpload = function (init_settings) {
this.initSWFUpload(init_settings);
};
SWFUpload.prototype.initSWFUpload = function (init_settings) {
// Remove background flicker in IE (read this: http://misterpixel.blogspot.com/2006/09/forensic-analysis-of-ie6.html)
// This doesn't have anything to do with SWFUpload but can help your UI behave better in IE.
try {
document.execCommand('BackgroundImageCache', false, true);
} catch (ex1) {
}
try {
this.customSettings = {}; // A container where developers can place their own settings associated with this instance.
this.settings = {};
this.eventQueue = [];
this.movieName = "SWFUpload_" + SWFUpload.movieCount++;
this.movieElement = null;
// Setup global control tracking
SWFUpload.instances[this.movieName] = this;
// Load the settings. Load the Flash movie.
this.initSettings(init_settings);
this.loadFlash();
this.displayDebugInfo();
} catch (ex2) {
this.debug(ex2);
}
}
/* *************** */
/* Static thingies */
/* *************** */
SWFUpload.instances = {};
SWFUpload.movieCount = 0;
SWFUpload.QUEUE_ERROR = {
QUEUE_LIMIT_EXCEEDED : -100,
FILE_EXCEEDS_SIZE_LIMIT : -110,
ZERO_BYTE_FILE : -120,
INVALID_FILETYPE : -130
};
SWFUpload.UPLOAD_ERROR = {
HTTP_ERROR : -200,
MISSING_UPLOAD_URL : -210,
IO_ERROR : -220,
SECURITY_ERROR : -230,
UPLOAD_LIMIT_EXCEEDED : -240,
UPLOAD_FAILED : -250,
SPECIFIED_FILE_ID_NOT_FOUND : -260,
FILE_VALIDATION_FAILED : -270,
FILE_CANCELLED : -280,
UPLOAD_STOPPED : -290
};
SWFUpload.FILE_STATUS = {
QUEUED : -1,
IN_PROGRESS : -2,
ERROR : -3,
COMPLETE : -4,
CANCELLED : -5
};
/* ***************** */
/* Instance Thingies */
/* ***************** */
// init is a private method that ensures that all the object settings are set, getting a default value if one was not assigned.
SWFUpload.prototype.initSettings = function (init_settings) {
// Upload backend settings
this.addSetting("upload_url", init_settings.upload_url, "");
this.addSetting("file_post_name", init_settings.file_post_name, "Filedata");
this.addSetting("post_params", init_settings.post_params, {});
// File Settings
this.addSetting("file_types", init_settings.file_types, "*.*");
this.addSetting("file_types_description", init_settings.file_types_description, "All Files");
this.addSetting("file_size_limit", init_settings.file_size_limit, "1024");
this.addSetting("file_upload_limit", init_settings.file_upload_limit, "0");
this.addSetting("file_queue_limit", init_settings.file_queue_limit, "0");
// Flash Settings
this.addSetting("flash_url", init_settings.flash_url, "swfupload.swf");
this.addSetting("flash_width", init_settings.flash_width, "1px");
this.addSetting("flash_height", init_settings.flash_height, "1px");
this.addSetting("flash_color", init_settings.flash_color, "#FFFFFF");
// Debug Settings
this.addSetting("debug_enabled", init_settings.debug, false);
// Event Handlers
this.flashReady_handler = SWFUpload.flashReady; // This is a non-overrideable event handler
this.swfUploadLoaded_handler = this.retrieveSetting(init_settings.swfupload_loaded_handler, SWFUpload.swfUploadLoaded);
this.fileDialogStart_handler = this.retrieveSetting(init_settings.file_dialog_start_handler, SWFUpload.fileDialogStart);
this.fileQueued_handler = this.retrieveSetting(init_settings.file_queued_handler, SWFUpload.fileQueued);
this.fileQueueError_handler = this.retrieveSetting(init_settings.file_queue_error_handler, SWFUpload.fileQueueError);
this.fileDialogComplete_handler = this.retrieveSetting(init_settings.file_dialog_complete_handler, SWFUpload.fileDialogComplete);
this.uploadStart_handler = this.retrieveSetting(init_settings.upload_start_handler, SWFUpload.uploadStart);
this.uploadProgress_handler = this.retrieveSetting(init_settings.upload_progress_handler, SWFUpload.uploadProgress);
this.uploadError_handler = this.retrieveSetting(init_settings.upload_error_handler, SWFUpload.uploadError);
this.uploadSuccess_handler = this.retrieveSetting(init_settings.upload_success_handler, SWFUpload.uploadSuccess);
this.uploadComplete_handler = this.retrieveSetting(init_settings.upload_complete_handler, SWFUpload.uploadComplete);
this.debug_handler = this.retrieveSetting(init_settings.debug_handler, SWFUpload.debug);
// Other settings
this.customSettings = this.retrieveSetting(init_settings.custom_settings, {});
};
// loadFlash is a private method that generates the HTML tag for the Flash
// It then adds the flash to the "target" or to the body and stores a
// reference to the flash element in "movieElement".
SWFUpload.prototype.loadFlash = function () {
var html, target_element, container;
// Make sure an element with the ID we are going to use doesn't already exist
if (document.getElementById(this.movieName) !== null) {
return false;
}
// Get the body tag where we will be adding the flash movie
try {
target_element = document.getElementsByTagName("body")[0];
if (typeof(target_element) === "undefined" || target_element === null) {
this.debug('Could not find the BODY element. SWFUpload failed to load.');
return false;
}
} catch (ex) {
return false;
}
// Append the container and load the flash
container = document.createElement("div");
container.style.width = this.getSetting("flash_width");
container.style.height = this.getSetting("flash_height");
target_element.appendChild(container);
container.innerHTML = this.getFlashHTML(); // Using innerHTML is non-standard but the only sensible way to dynamically add Flash in IE (and maybe other browsers)
};
// Generates the embed/object tags needed to embed the flash in to the document
SWFUpload.prototype.getFlashHTML = function () {
var html = "";
// Create Mozilla Embed HTML
if (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length) {
// Build the basic embed html
html = '<embed type="application/x-shockwave-flash" src="' + this.getSetting("flash_url") + '" width="' + this.getSetting("flash_width") + '" height="' + this.getSetting("flash_height") + '"';
html += ' id="' + this.movieName + '" name="' + this.movieName + '" ';
html += 'bgcolor="' + this.getSetting("flash_color") + '" quality="high" menu="false" flashvars="';
html += this.getFlashVars();
html += '" />';
// Create IE Object HTML
} else {
// Build the basic Object tag
html = '<object id="' + this.movieName + '" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="' + this.getSetting("flash_width") + '" height="' + this.getSetting("flash_height") + '">';
html += '<param name="movie" value="' + this.getSetting("flash_url") + '">';
html += '<param name="bgcolor" value="' + this.getSetting("flash_color") + '" />';
html += '<param name="quality" value="high" />';
html += '<param name="menu" value="false" />';
html += '<param name="flashvars" value="' + this.getFlashVars() + '" />';
html += '</object>';
}
return html;
};
// This private method builds the parameter string that will be passed
// to flash.
SWFUpload.prototype.getFlashVars = function () {
// Build a string from the post param object
var param_string = this.buildParamString();
// Build the parameter string
var html = "";
html += "movieName=" + encodeURIComponent(this.movieName);
html += "&uploadURL=" + encodeURIComponent(this.getSetting("upload_url"));
html += "¶ms=" + encodeURIComponent(param_string);
html += "&filePostName=" + encodeURIComponent(this.getSetting("file_post_name"));
html += "&fileTypes=" + encodeURIComponent(this.getSetting("file_types"));
html += "&fileTypesDescription=" + encodeURIComponent(this.getSetting("file_types_description"));
html += "&fileSizeLimit=" + encodeURIComponent(this.getSetting("file_size_limit"));
html += "&fileUploadLimit=" + encodeURIComponent(this.getSetting("file_upload_limit"));
html += "&fileQueueLimit=" + encodeURIComponent(this.getSetting("file_queue_limit"));
html += "&debugEnabled=" + encodeURIComponent(this.getSetting("debug_enabled"));
return html;
};
SWFUpload.prototype.getMovieElement = function () {
if (typeof(this.movieElement) === "undefined" || this.movieElement === null) {
this.movieElement = document.getElementById(this.movieName);
// Fix IEs "Flash can't callback when in a form" issue (http://www.extremefx.com.ar/blog/fixing-flash-external-interface-inside-form-on-internet-explorer)
// Removed because Revision 6 always adds the flash to the body (inside a containing div)
// If you insist on adding the Flash file inside a Form then in IE you have to make you wait until the DOM is ready
// and run this code to make the form's ID available from the window object so Flash and JavaScript can communicate.
//if (typeof(window[this.movieName]) === "undefined" || window[this.moveName] !== this.movieElement) {
// window[this.movieName] = this.movieElement;
//}
}
return this.movieElement;
};
SWFUpload.prototype.buildParamString = function () {
var post_params = this.getSetting("post_params");
var param_string_pairs = [];
var i, value, name;
// Retrieve the user defined parameters
if (typeof(post_params) === "object") {
for (name in post_params) {
if (post_params.hasOwnProperty(name)) {
if (typeof(post_params[name]) === "string") {
param_string_pairs.push(encodeURIComponent(name) + "=" + encodeURIComponent(post_params[name]));
}
}
}
}
return param_string_pairs.join("&");
};
// Saves a setting. If the value given is undefined or null then the default_value is used.
SWFUpload.prototype.addSetting = function (name, value, default_value) {
if (typeof(value) === "undefined" || value === null) {
this.settings[name] = default_value;
} else {
this.settings[name] = value;
}
return this.settings[name];
};
// Gets a setting. Returns empty string if not found.
SWFUpload.prototype.getSetting = function (name) {
if (typeof(this.settings[name]) === "undefined") {
return "";
} else {
return this.settings[name];
}
};
// Gets a setting, if the setting is undefined then return the default value
// This does not affect or use the interal setting object.
SWFUpload.prototype.retrieveSetting = function (value, default_value) {
if (typeof(value) === "undefined" || value === null) {
return default_value;
} else {
return value;
}
};
// It loops through all the settings and displays
// them in the debug Console.
SWFUpload.prototype.displayDebugInfo = function () {
var key, debug_message = "";
debug_message += "----- SWFUPLOAD SETTINGS ----\nID: " + this.moveName + "\n";
debug_message += this.outputObject(this.settings);
debug_message += "----- SWFUPLOAD SETTINGS END ----\n";
debug_message += "\n";
this.debug(debug_message);
};
SWFUpload.prototype.outputObject = function (object, prefix) {
var output = "", key;
if (typeof(prefix) !== "string") {
prefix = "";
}
if (typeof(object) !== "object") {
return "";
}
for (key in object) {
if (object.hasOwnProperty(key)) {
if (typeof(object[key]) === "object") {
output += (prefix + key + ": { \n" + this.outputObject(object[key], "\t" + prefix) + prefix + "}" + "\n");
} else {
output += (prefix + key + ": " + object[key] + "\n");
}
}
}
return output;
};
/* *****************************
-- Flash control methods --
Your UI should use these
to operate SWFUpload
***************************** */
SWFUpload.prototype.selectFile = function () {
var movie_element = this.getMovieElement();
if (movie_element !== null && typeof(movie_element.SelectFile) === "function") {
try {
movie_element.SelectFile();
}
catch (ex) {
this.debug("Could not call SelectFile: " + ex);
}
} else {
this.debug("Could not find Flash element");
}
};
SWFUpload.prototype.selectFiles = function () {
var movie_element = this.getMovieElement();
if (movie_element !== null && typeof(movie_element.SelectFiles) === "function") {
try {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -