var swfu;
window.οnlοad=function() {
var uploadUrl = "/resource/swfupload.do";
//在firefox、chrome下,上传不能保留登录信息,所以必须加上jsessionid。
var jsessionid = $.cookie("JSESSIONID");
if(jsessionid) {
uploadUrl += ";jsessionid="+jsessionid;
}
swfu=new SWFUpload({
upload_url : uploadUrl,
flash_url : "/resources/ztstyle/swfupload.swf",
file_size_limit : "1000 MB",
post_params: {"root" : "${root}"},
file_types : "*.*",
file_types_description : "All Files",
file_queue_limit : 0,
custom_settings : {
progressTarget : "fsUploadProgress",
cancelButtonId : "btnCancel"
},
debug: false,
button_image_url : "/resources/ztstyle/button_notext.png",
button_placeholder_id : "spanButtonPlaceHolder",
button_text: "上传文件",
button_width: 61,
button_height: 22,
file_queued_handler : fileQueued,
file_queue_error_handler : fileQueueError,
file_dialog_complete_handler : fileDialogComplete,//当文件选取对话框关闭后触发的事件处理函数
upload_start_handler : uploadStart,
upload_progress_handler : uploadProgress,
upload_error_handler : uploadError,
upload_success_handler : uploadSuccess,
upload_complete_handler : uploadComplete,
queue_complete_handler : queueComplete2
});
}
function queueComplete2(numFilesUploaded) {
var root = $("#nowRoot").val();
//刷新页面
location.href="/resource/list.do?root="+root;
}
如果你需要动态修改,
post_params: {"root" : "${root}"},
可以通过SWFUplaod提供的一系列的handler事件,在上传文件的不同过程中触发,我选择的是file_dialog_complete_handler(文件上传框关闭触发,修改上传路径),你只需要在SWFUplaod的handler.js中做如下修改:
//资源管理上传文件之前修改上传路径
function fileDialogComplete(numFilesSelected, numFilesQueued) {
try {
if (numFilesSelected > 0) {
document.getElementById(this.customSettings.cancelButtonId).disabled = false;
}
//修改文件上传路径
var root = $("#nowRoot").val();//获取当前路径
var postobj = { "root": root};
swfu.setPostParams(postobj);//设置路径
//设置自动上传
this.startUpload();
} catch (ex) {
this.debug(ex);
}
}
这样就可以了。
如果你想在一个页面创建多个SWFUplaod,需要在swfupload.js中做如下修改:
SWFUpload.prototype.initSWFUpload = function (settings) {
try {
this.customSettings = {}; // A container where developers can place their own settings associated with this instance.
this.settings = settings;
this.eventQueue = [];
//修改允许创建多个SWFUplaod
var date = new Date();
this.movieName = "SWFUpload_" + date.getTime().toString();
this.movieElement = null;
// Setup global control tracking
SWFUpload.instances[this.movieName] = this;
// Load the settings. Load the Flash movie.
this.initSettings();
this.loadFlash();
this.displayDebugInfo();
} catch (ex) {
delete SWFUpload.instances[this.movieName];
throw ex;
}
};
生活不止眼前的苟且,还有诗和远方。加油吧。