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).οnlοad=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;
}
ajax 文件上传
最新推荐文章于 2023-03-16 12:05:09 发布