jquery ajax上传

/**
 *  author: leunpha
 *  dependency: jquery.js
 *  date: 2014-5-1
 *  version 2.0
 */

$.fn.upload = function (options) {
    options = options || {};
    options.dom = this;
    $.upload(options);
}
$.upload = function (options) {
    var settings = {
        dom: "",
        action: "",
        fileName: "file",
        params: {},
        accept: ".jpg,.png",
        ieSubmitBtnText: "上传",
        complete: function () {},
        submit: function () {

        }
    }
    settings = $.extend(settings,options);
    var ele = settings.dom;

    var iframeName = "leunpha_iframe_v"+Math.random()*100;
    var width = ele.outerWidth();
    var height = ele.outerHeight();
    var iframe = $(">");
    iframe.attr({
        "name": iframeName,
        id: iframeName
    }).css({
            display: "none"
        });
    var form = $("
   
   
"); form.attr({ target: iframeName, action: settings.action, method: "post", "class":"ajax_form", enctype: "multipart/form-data" }).css({ width:width, height:height, position:"absolute", top: (ele.offset().top), left: (ele.offset().left) }); var input = $(""); input.attr({ accept:settings.accept, name: settings.fileName }) .css({ opacity: 0, position: "absolute", width: width, height: height + "px", cursor:"pointer" }); input.change(function () { settings.submit.call(form); $(this).parent("form").submit(); }); form.append(input); $("body").append(iframe); iframe.after(form); for (var param in settings.params) { var div = $("").attr({name: param, value: settings.params[param]}); input.after(div) div = null; delete div; } iframe.load(function () { var im = document.getElementById(iframeName); var text = $(im.contentWindow.document.body).text(); if (text) { settings.complete.call(null, text); } }); }


jquery .upload.leunpha.js 使用方式:导入js 然后
$(dom).upload({
    action: "",
    fileName: "file",
    params: {},
    accept: ".jpg,.png",
    ieSubmitBtnText: "上传",
    complete: function () {
        //上传完成
    }
});


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值