openuploader.js

来自「asp的bbs程序」· JavaScript 代码 · 共 208 行

JS
208
字号
var _flashEnabled = Browser.IsIE ? Global.FlashEnabled() : false;
var _ext = '';
if (typeof(staticExt) !== undefined) _ext = staticExt;
if (_flashEnabled) {
    Global.LoadScript(root + '/common/scripts/functions/upload.swf.js' + _ext);
    Global.LoadScript(root + '/common/scripts/functions/handlers.js' + _ext);
}
else {
    Global.LoadScript(root + '/common/scripts/lib/controls/ofileupload.lib.js' + _ext);
    Global.LoadScript(root + '/common/scripts/functions/upload.js' + _ext);
}
/*
    obj = {
        Button : HTMLElement,
        ButtonWidth : Number,
        ButtonHeight : Number,
        ButtonText : String,
        ButtonTextStyle : String,
        ButtonImage : String,
        FileSizeLimit : Number,
        ///
        Type : '',
        DirectoryID : '',
        Path : '',
        QueryString : '',
        // upload
        UploadUrl : '',
        // (HTML)
        GetFileIdUrl : '',
        GetFileInfoUrl : '',
        // (FLASH)
        RenameFileUrl : '',
        CheckFileNameUrl : '',
        // extends attributes
        ExtList : '',
        // (HANDLER)
        CloseUploader : Function,
        SelectedOne : Function,
        SelectedAll : Function,
        DialogClosed : Function,
        Render : Function,
        CloseUploader : Function,
    }
*/
function openUpload (obj) {
    var DirectoryID = obj.DirectoryID ? '&DirectoryID=' + Url.Escape(obj.DirectoryID) : '';
    var Path = obj.Path ? '&Path=' + Url.Escape(obj.Path) : '';

    DiskSettingOpenDisk = typeof DiskSettingOpenDisk == 'undefined'
        ? true
        : !!DiskSettingOpenDisk;

    if (_flashEnabled) {
        var settings = {
            flash_url : root + '/common/scripts/functions/swfupload.swf',
            upload_url: 'upload.php',	// Relative to the SWF file
            //post_params: {"PHPSESSID" : "<?php echo session_id(); ?>"},
            file_size_limit : obj.FileSizeLimit,
            file_types : obj.ExtList,
            file_types_description : 'All Files',
            file_upload_limit : 10000,
            file_queue_limit : 0,
            /*
            custom_settings : {
                progressTarget : "fsUploadProgress",
                cancelButtonId : "btnCancel"
            },
            */
            debug: false,
            
            // Button settings
			button_image_url: obj.ButtonImage,	// Relative to the Flash file
			button_width: obj.ButtonWidth,
			button_height: obj.ButtonHeight,
			button_placeholder_id: obj.Button.id,
			button_text: obj.ButtonText,
			button_text_style: obj.ButtonTextStyle,
			button_text_left_padding: 0,
			button_text_top_padding: 2, 
			button_cursor: SWFUpload.CURSOR.HAND,
            button_window_mode: SWFUpload.WINDOW_MODE.OPAQUE,

            // The event handler functions are defined in handlers.js
            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 : queueComplete	// Queue plugin event
        };

        var swfu = new SWFUpload(settings);
        obj.Button.onclick = function () {
            swfu.selectFiles();
        };
        
        _Upload.DocumentTitle = document.title;
        _Upload.UploadUrl = obj.UploadUrl;
        _Upload.CheckFileNameUrl = obj.CheckFileNameUrl;
        _Upload.RenameUrl = obj.RenameFileUrl;
        _Upload.DiskSettingOpenDisk = DiskSettingOpenDisk;
        _Upload.FileSizeLimit = obj.FileSizeLimit;
        _Upload.Type = obj.Type;
        _Upload.DirectoryID = DirectoryID;
        _Upload.Path = Path;
        _Upload.QueryString = obj.QueryString;
        _Upload.SelectedOne = function (filename) {
            if (typeof obj.SelectedOne === 'function')
                return obj.SelectedOne(this, filename);
            return 1;
        };
        _Upload.SelectedAll = function () {
            if (typeof obj.SelectedAll === 'function')
                return obj.SelectedAll(this);
            return true;
        };
        _Upload.DialogClosed = function () {
            if (typeof obj.DialogClosed === 'function')
                obj.DialogClosed();
        };
        _Upload.Render = function () {
            Widget.Resize(true, true);
            //if (typeof obj.Render === 'function')
            //    obj.Render();
        };
        _Upload.UploadedOne = function (filename, content, item) {
            if (typeof obj.UploadedOne === 'function')
                return obj.UploadedOne(this, filename, content, item);
            return null;
        };
        _Upload.UploadedAll = function () {
            Widget.Close();
        };
        _Upload.CloseUploader = obj.CloseUploader || (function () {});
    }
    else {
        var ofu = Upload.UploadFile(obj.Button);
        ofu.Click = function (e) {
            Widget.Content('上传文件进度', $('file_upload'), 500, function () {
                Upload.RemoveAll();
                openUpload(obj);
                typeof obj.CloseUploader === 'function' &&
                obj.CloseUploader();
                return true;
            });
            
            Upload.UploadUrl = obj.UploadUrl;
            Upload.AjaxUrl = obj.GetFileInfoUrl;
            Upload.GetIdUrl = obj.GetFileIdUrl;
            Upload.Type = obj.Type;
            
            Upload.DiskSettingOpenDisk = DiskSettingOpenDisk;
            
            Upload.DirectoryID = DirectoryID;
            formatDirectoryID(Upload);
            
            Upload.Path = Path;
            Upload.QueryString = obj.QueryString;
            
            // 选择一个上传文件之后
            Upload.SelectedOne = function (filename) {
                if (typeof obj.SelectedOne === 'function')
                    return obj.SelectedOne(this, filename);
                return 1;
            };
            // 渲染一个上传文件条目
            Upload.Render = function () {
                Widget.Resize(true, true);
                //if (typeof obj.Render === 'function')
                //    obj.Render();
            };
            // 取消一个上传队列文件
            Upload.DeletedOne = function (count) {};
            // 上传完一文件
            Upload.UploadedOne = function (filename, content, item) {
                if (typeof obj.UploadedOne === 'function')
                    return obj.UploadedOne(this, filename, content, item);
                return null;
            };
            Upload.UploadedAll = function (closeable) {
                if (closeable) Widget.Close();
            };
            Upload();
            Upload.Hide();
        };
        var interval;
        var func = function () {
            Widget.Show();
            Widget.Close();
            clearInterval(interval);
        };
        Events.AttachEvent(window, 'focus', function () {
            if (Browser.IsIE && Upload.IsHide && !Upload.oFileUpload.Closed && Upload.Count > 0) {
                interval = setInterval(func, 1000);
            }
        });
    }
}

function formatDirectoryID (upload) {
    if ($('hidden_CurrentDirectoryDirectoryID')) {
        upload.DirectoryID = '&DirectoryID=' + Url.Escape($('hidden_CurrentDirectoryDirectoryID').value);
        upload.Path = '';
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?