無重新整理上傳圖片,ajax 和 iframe

來源:互聯網
上載者:User

標籤:上傳圖片   無重新整理   

iframe 上傳


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" style="width:0;heigth:0;overflow:hidden;border:0;position: absolute; left:-500px;"></iframe><img id="tag_img" src="http://avatar.csdn.net/C/2/1/1_shenlingsuifeng.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" style="font-size: 12px;margin-left: 10px;width: 70px;height: 30px;background: #10AD5A;color: #fff;">修改頭像</button><input type="file" name="pic" id="pic" hidden accept="image/*" /><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,// 需要加這兩個參數      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("上傳失敗,請檢查網路後重試");      }    });  });</script>

對應uploadimg方法

public function uploadimg() {   $file = $_FILES[‘file‘];   $arr = array(‘jpg‘=>‘image/jpeg‘,‘png‘=>‘image/png‘,‘gif‘=>‘image/gif‘,‘bmp‘=>‘image/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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.