swfupload上传方法封装

102 篇文章 0 订阅
function fileQueueError(file, errorCode, message) {
    try {
        switch (errorCode) {
            case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
                art.dialog({ content: "文件过大,请选择500k以下的文件", title: "错误", lock: true, width: 350, icon: "error" });
                break;
            case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
                art.dialog({ content: "无法上传零字节文件", title: "错误", lock: true, width: 350, icon: "error" });
                break;
            case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
                art.dialog({ content: "不支持的文件类型", title: "错误", lock: true, width: 350, icon: "error" });
                break;
            default:
                if (file !== null) {
                    art.dialog({ content: "未处理的错误", title: "错误", lock: true, width: 350, icon: "error" });
                }
                break;
        }
    } catch (ex) {
        this.debug(ex);
    }
}


function fileDialogComplete(numFilesSelected, numFilesQueued) {
    try {
        var obj = this;
        if (numFilesQueued > 0) {
            
                obj.startUpload();
        }
    } catch (ex) {
        this.debug(ex);
    }
}


function uploadProgress(file, bytesLoaded) {
    try {
        
    } catch (ex) {
        this.debug(ex);
    }
}


function uploadSuccess(file, serverData) {
    try {
        var json = eval("(" + serverData + ")");
        if (json.err == "0") {
            $("#img").attr("src", json.data);
            $("#himg").val(json.data);
        }
        else {
            art.dialog({ content: json.msg, title: "错误", lock: true });
        }


    } catch (ex) {
        this.debug(ex);
    }
}


function uploadComplete(file) {
    try {
        if (this.getStats().files_queued > 0) {
            this.startUpload();
        }


    } catch (ex) {
        this.debug(ex);
    }
}


function uploadError(file, errorCode, message) {
    try {
        switch (errorCode) {
            case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
                try {
                }
                catch (ex1) {
                    this.debug(ex1);
                }
                break;
            case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
                    try {
                    }
                    catch (ex2) {
                        this.debug(ex2);
                    }
            case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
                break;
            default:
                break;
        }
    } catch (ex3) {
        this.debug(ex3);
    }
}




function InitMySwf(settings, swfObj) {
    if (settings.buttonId == undefined) {
        art.dialog({ title: "错误", content: "请给上传控件指定容器ID", lock: true, drag: false });
        return;
    }
    var upload_url = "/handler/UpLoadImgFile.ashx";
    if (settings.upload_url != undefined) {
        upload_url = settings.upload_url;
    }
    var file_queue_error_handler = fileQueueError;
    if (settings.file_queue_error_handler != undefined) {
        file_queue_error_handler = settings.file_queue_error_handler;
    }
    var upload_progress_handler = uploadProgress;
    if (settings.upload_progress_handler != undefined) {
        upload_progress_handler = settings.upload_progress_handler;
    }


    var upload_error_handler = uploadError;
    if (settings.upload_error_handler != undefined) {
        upload_progress_handler = settings.upload_progress_handler;
    }
    var upload_success_handler = uploadSuccess;
    if (settings.upload_success_handler != undefined) {
        upload_success_handler = settings.upload_success_handler;
    }
    var upload_complete_handler = uploadComplete;
    if (settings.upload_complete_handler != undefined) {
        upload_complete_handler = settings.upload_complete_handler;
    }
    var file_dialog_complete_handler = fileDialogComplete;
    if (settings.file_dialog_complete_handler != undefined) {
        file_dialog_complete_handler = settings.file_dialog_complete_handler;
    }
    var button_width = 72;
    if (settings.button_width != undefined) {
        button_width = settings.button_width;
    }
    var button_height = 29;
    if (settings.button_height != undefined) {
        button_height = settings.button_height;
    }
    var filetype = "*.jpg;*.png;*.gif";
    if (settings.file_types != undefined) {
        filetype = settings.file_types;
    }
    var action = "upload";
    if (settings.action != undefined) {
        action = settings.action;
    }
    
    var buttonId = settings.buttonId;
    swfObj = new SWFUpload({
        // 基本设置
        upload_url: upload_url,
        post_params: {
            "ASPSESSID": Math.random(),
            ac: action
        },
        // 上传文件设置
        file_size_limit: "0",
        file_types: filetype,
        file_types_description: "支持上传的格式",
        file_upload_limit: "500",    // 0表示不限制选择文件的数量


        // 定义事件				
        file_queue_error_handler: file_queue_error_handler,
        file_dialog_complete_handler: file_dialog_complete_handler,
        upload_progress_handler: upload_progress_handler,
        upload_error_handler: upload_error_handler,
        upload_success_handler: upload_success_handler,
        upload_complete_handler: upload_complete_handler,


        // 按钮设置
        button_placeholder_id: buttonId,
        button_width: button_width,
        button_height: button_height,
        button_cursor: SWFUpload.CURSOR.HAND,
        button_window_mode: "transparent",
        button_text_top_padding: 0,
        button_text_left_padding: 0,


        // Flash设置,下面三行必须有
        flash_url: "/swfupload/swfupload.swf",


        //跨域
        prevent_swf_caching: false,
        preserve_relative_urls: false,


        // 是否开启调试,true是,false否
        debug: false
    });
}

使用时:

var swfu;
$(function () {
    var myswfsetting = {
        buttonId: "spanButtonPlaceholder1"
    };
    InitMySwf(myswfsetting, swfu);
    //myswfbox 
});

本方法中用了artdialog的弹窗,因此调用时,请保证在本脚本前引用了artdialog

顺便粘一下后台处理的代码:

ajaxMsgModel rm = new ajaxMsgModel { err = 1, msg = "非法提交" };
            string ac = context.Request["ac"];
            if (ac == "upload")
            {
                try
                {
                    HttpPostedFile pfile = context.Request.Files["Filedata"];
                    if (pfile == null)
                    {
                        return;
                    }
                    string fileformat = pfile.FileName.Substring(pfile.FileName.LastIndexOf(".")).ToLower() ;
                    string fileflx = ",.zip,.rar,.doc,.docx,.xls,.xlsx,";
                    if (!fileflx.Contains(fileformat))
                    {
                        return;
                    }
                    
                    DateTime dtime = DateTime.Now;
                    string filename = string.Format("{0:yyyyMMddHHmmss}", dtime) + fileformat;
                    string folder = context.Server.MapPath("/upload/");
                    if (!Directory.Exists(folder))
                    {
                        IOHelper.CreateFoder(folder);
                    }
                    
                    string filepath = folder + filename;
                    //保存文件
                    pfile.SaveAs(filepath);
                    rm = new ajaxMsgModel { err = 0, msg = filename,data="/upload/"+filename };
                    return;
                }
                catch (Exception ex)
                {
                    rm = new ajaxMsgModel { err = 1, msg = ex.Message };
                }
                finally
                {
                    string msg = OutputHelper.GetJsonMsg(rm);
                    context.Response.Write(msg);
                }


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值