前台页面:
<iframe
name="upload_hidden_frame" id="upload_hidden_frame"frameborder="0" style="display: none;"></iframe>
<form
action="<?php echo $base_url . $task; ?>" method="post"
enctype="multipart/form-data" id="upload-form"
target="upload_hidden_frame" >
<div class="file_url_div l">
<input id="file_url" name="file_url" type="text"/>
</div>
<div class="file_text_div l">
<input id="file_windows_btn" type="button" value="选择图片"/>
<input id="file_value"
name="file_value"
type="file" style="display: none;"/>
</div>
<div class="clear"></div>
<input id="upload_submit" type="submit" href="javascript:void(0)" style="display: none;"/>
</form>
后台处理图片上传成功后的回调处理机制:
if(move_uploaded_file($tmp_name,$new_url .$new_name)){
$width_height = getimagesize($new_url .$new_name);
$width = $width_height[0];
$height = $width_height[1];
$destination['result'] = true;
$destination['width'] = $width;
$destination['height'] = $height;
$destination['name'] = $name;
$destination['success']= '图片上传成功 !';
$destination['src'] = JUri::root(true) .'/media/com_projectfork/projectfork/upload_requirement_file/'.$new_name;
}
$result = "<script>window.parent.
uploadCallback(".json_encode($destination).")</script>";
echo $result;
jexit();
前台JS获取回调信息:
function
uploadCallback(data){ //data:后台回调数据,根据data获取的信息,可执行异步刷新的图片的效果
if(data.result){
var width = "";
var img = "";
jQuery("#upload_remind_div > #img_size_type").find("span").text("");
jQuery("#upload_remind_div > #img_size_type").find("span").text(data.success);
if(data.width>400){
width = "470";
img = jQuery("<img>",{"src":data.src }).css({
"width":width
});
}
else{
img = jQuery("<img>",{"src":data.src });
}
var cont = jQuery(window.frames["ke-edit-iframe-upload"].document).find("body");
if(cont.length!=0){
var p = jQuery("<p></p>");
var a = jQuery("<a>文件名称:"+data.name+"</a>").css({
"margin-left":"auto",
"margin-right":"auto"
}).attr("href",data.src);
p.append(img);
cont.append(p).append(a);
}
}
else{
jQuery("#upload_remind_div > #img_size_type").find("span").text("");
jQuery("#upload_remind_div > #img_size_type").find("span").text(data.src);
}
}