ajax 文件上传

来源:互联网 发布:itools for mac录屏 编辑:程序博客网 时间:2024/06/10 05:30
function updateImage(o,id){var $this = o;var $form=createForm(id);        var $iframe=createIframe(id);        $($iframe.get(0).contentWindow.document.body).append($form);                $($form.find("input[name=file]")).change(function(e){        e.stopPropagation();            e.stopImmediatePropagation();                        if(this.files.length>0&&this.files[0]){            var file=this.files[0];                        if(file.size>(1*1024*1024)){            layer.msg("亲...您上传的文件大小不能超过1M!");                    return;                }                            if(!/image\/(gif|png|jpeg|jpg)/.test(file.type)){                layer.msg("亲...文件类型错误,仅支持GIF、JPG、PNG格式图片!");                    return;                }                                //提交                $form.submit();                isUploading=true;                                $iframe.get(0).onload=function(e){                    var responseText=$(this.contentWindow.document.body).text();                    if(responseText){                        var responseObj=JSON.parse(responseText);                        if(responseObj.uploadImage=="success"){                        $($this).data("buffer",responseObj);                        var path = responseObj.relativePath;                        var url = "$!webpath/printApplyController/showImage.htm?path="+path;                        $($this).find("img").attr("src",url);                        layer.msg("图片上传成功!");                        }else{                        layer.msg("图片上传失败!");                        }                    }                    $form.remove();                    $iframe.remove();                    isUploading=false;               }            }        });        $form.find("input[name='file']").click();}function createForm(id){    var $fileUpload=$("body").find("form[name='form_"+id+"']");    if($fileUpload.length==0){        var uploadPath="$!webpath/printApplyController/uploadPrintApply.htm";        var form="<form name='form_"+id+"'  enctype='multipart/form-data' method='POST' action='"+uploadPath+"'>" +                 "<input type='file' name='file' style='display:none;' />" +                 "</form>";        $fileUpload=$(form)        $("body").append($fileUpload);    }    return $fileUpload;}function createIframe(id){    var $iframe=$("iframe[name='iframe_"+id+"']");   if($iframe.length==0){        var html="<iframe id=\"iframe_"+id+"\" name=\"iframe_"+id+"\" src=\"about:blank\" style='display:none;'></iframe>"        $iframe=$(html);        $("body").append($iframe);    }     return $iframe;}

原创粉丝点击