iframe 上傳
創(chuàng)新互聯(lián)建站專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站制作、成都做網(wǎng)站、洮南網(wǎng)絡(luò)推廣、微信小程序定制開發(fā)、洮南網(wǎng)絡(luò)營銷、洮南企業(yè)策劃、洮南品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)建站為所有大學(xué)生創(chuàng)業(yè)者提供洮南建站搭建服務(wù),24小時服務(wù)熱線:13518219792,官方網(wǎng)址:www.rwnh.cn
upload.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script> </head> <body> <iframe id="upload_target" name="upload_target" src="upload.php" ></iframe> <img id="tag_img" src="/upload/otherpic59/107580.jpg" /> <form enctype="multipart/form-data" action="upload.php" method="post" target="upload_target"> <input type="file" id="fileipt" name="userfile" class="file" value="" /> <input type="submit" name="uploadimg" value="上傳" id="submit" hidden /> </form> <script type='text/javascript'> var lastFileName; $("#fileipt").change(function() { var fileName = $(this).val(); var pos = fileName.lastIndexOf("\\"); fileName = fileName.substr(pos+1, fileName.length); // 截取出文件名 因為會帶路徑 lastFileName = fileName; $("#submit").click(); }); function stopSend($url) { $("#tag_img").attr("src",$url); } </script> </body> </html>
upload.php
<?php /** * Created by PhpStorm. * User: chenxiaolong * Date: 7/21/17 * Time: 10:24 */ //var_dump($_FILES); $file=$_FILES['userfile']; if($file['size'] != 0) { $name=rand(0,500000).dechex(rand(0,10000)).".jpg"; move_uploaded_file($file['tmp_name'],$name); if($name) { echo "<script>parent.stopSend('$name')</script>"; } }
ajax 無刷新上傳圖片
<button id="J_headimg" >修改頭像</button> <input type="file" name="pic" id="pic" hidden accept="p_w_picpath/*" /> <input type="text" id="headimg" name="headimg" hidden> <script> $("#J_headimg").click(function() { $("#pic").click(); return false; }); $("#pic").change(function() { var $that = $(this); var imgPath = $(this).val(); if (imgPath == "") { alert("請選擇上傳圖片!"); return; } //判斷上傳文件的后綴名 var strExtension = imgPath.substr(imgPath.lastIndexOf('.') + 1); if (strExtension != 'jpg' && strExtension != 'gif' && strExtension != 'png' && strExtension != 'bmp' && strExtension != 'jpeg') { alert("請選擇圖片文件"); return; } var formData = new FormData(); formData.append('file', $that[0].files[0]);// php 用$_FILES['file']接收 console.log($that[0].files[0]); $.ajax({ type: "POST", url: "__URL__/uploadimg", data: formData, cache: false, processData: false,// 需要加這兩個參數(shù) contentType: false, success: function(data) { var obj = JSON.parse(data); if(obj.status == 0) { $("#preimg").attr("src","Public/Upload/" + obj.data); $("#headimg").val(obj.data); } else { alert(obj.data); } }, error: function(XMLHttpRequest, textStatus, errorThrown) { alert("上傳失敗,請檢查網(wǎng)絡(luò)后重試"); } }); }); </script>
對應(yīng)uploadimg方法
public function uploadimg() { $file = $_FILES['file']; $arr = array('jpg'=>'p_w_picpath/jpeg','png'=>'p_w_picpath/png','gif'=>'p_w_picpath/gif','bmp'=>'p_w_picpath/bmp'); if($ext = array_search($file['type'],$arr)) { $rand = uniqid(); $filename = "Public/Upload/avatar/{$rand}.{$ext}"; } else { exit(json_encode(array('status'=>2,'data'=>'只支持圖片上傳'))); } $r = move_uploaded_file($file['tmp_name'],$filename); if($r) { exit(json_encode(array('status'=>0,'data'=>"avatar/$rand.$ext"))); } else { exit(json_encode(array('status'=>1,'data'=>'上傳失敗'))); } }
文章名稱:無刷新上傳圖片,ajax和iframe
文章網(wǎng)址:http://www.rwnh.cn/article2/jdcioc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、Google、服務(wù)器托管、關(guān)鍵詞優(yōu)化、域名注冊、商城網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)