日期:2014-05-16 浏览次数:20619 次
<form id="formImg" action="uploadImg" method="post" target="hidden_frame" enctype="multipart/form-data"> <div class="fromtrs"> <label>[i]*[/i]活动海报:</label> <input id="AidImg" class="sstp" type="file" name="AidImg" onchange="uploadImg()"/> <div class="sjjt" style="display:none;" id="imgError"> <div class="bzsj_left"></div>活动海报不可为空</div> </div> </form> <iframe style="display:none" name='hidden_frame' id="hidden_frame"></iframe> <div class="fromtrs"> <img id="img" src="" width="80" height="80"/> </div>
public function uploadImg() { try{ if($_FILES["AidImg"]["size"]!=0) { $uploadPath = MB_ROOT_DIR . "/pic"; if ($_FILES["AidImg"]["size"] < 1024 * 1024 * 2) { if ($_FILES["AidImg"]["error"] > 0) { error($_FILES["AidImg"]["error"],"index.php/Campaign/campaignPublish"); } else { $suffix = substr($_FILES["AidImg"]["name"], strrpos($_FILES["AidImg"]["name"], '.') + 1); $imgDate=date("YmdHis"); $name = $imgDate . rand("1000", "9999") . "." . $suffix; if (!is_dir($uploadPath)) { mkdir($uploadPath); } if (move_uploaded_file($_FILES["AidImg"]["tmp_name"], $uploadPath . "/" . $name)) { $pf = new IBPostFile(UPLOAD_IMAGE_URL,UPLOAD_IMAGE_PORT); $pf->setFile("AidImg", $uploadPath . "/" . $name); $pf->sendRequest(); $imgUrl = $pf->getResponse(); } } } else { echo "<script>parent.callback('图片大小不能超过2M',false)</script>"; return; } } } catch(Exception $e) { echo "<script>parent.callback('图片上传失败',false)</script>"; return; } echo "<script>parent.callback('$imgUrl',true)</script>"; }
function uploadImg() { var names=$("#AidImg").val().split("."); if(names[1]!="gif"&&names[1]!="GIF"&&names[1]!="jpg"&&names[1]!="JPG"&&names[1]!="png"&&names[1]!="PNG") { $("#imgError").html("<div class='bzsj_left'></div>"+"海报必须为gif,jpg,png格式"); $("#imgError").show(); return; } $("#formImg").submit(); } function callback(message,success) { if(success==false) { $("#imgError").html("<div class='bzsj_left'></div>"+message); $("#imgError").show(); } else{ $("#imgError").hide(); var paths=message.split("/"); $("#img").attr("src",message); $("input[name='imgUrl']").val(paths[paths.length-1]); } }