php+html5+ajax實現上傳圖片的方法及執行個體

來源:互聯網
上載者:User
這篇文章主要介紹了php+html5+ajax實現上傳圖片的方法,對比分析了js原生及jQuery兩種ajax調用上傳圖片的方法,以及php圖片上傳處理等技巧,需要的朋友可以參考下

具體如下:

<?phpif (isset($_POST['upload'])) {  var_dump($_FILES);  move_uploaded_file($_FILES['upfile']['tmp_name'], 'up_tmp/'.time().'.dat');  //header('location: test.php');  exit;}?>

<!doctype html><html lang="zh"><head>  <meta charset="utf-8">  <title>HTML5 Ajax Uploader</title>  <script src="jquery-2.1.1.min.js"></script></head><body><p><input type="file" id="upfile"></p><p><input type="button" id="upJS" value="用原生JS上傳"></p><p><input type="button" id="upJQuery" value="用jQuery上傳"></p><script>  /*原生JS版*/  document.getElementById("upJS").onclick = function() {    /* FormData 是表單資料類 */    var fd = new FormData();    var ajax = new XMLHttpRequest();    fd.append("upload", 1);    /* 把檔案添加到表單裡 */    fd.append("upfile", document.getElementById("upfile").files[0]);    ajax.open("post", "test.php", true);    ajax.onload = function () {      console.log(ajax.responseText);    };    ajax.send(fd);  }  /* jQuery 版 */  $('#upJQuery').on('click', function() {    var fd = new FormData();    fd.append("upload", 1);    fd.append("upfile", $("#upfile").get(0).files[0]);    $.ajax({      url: "test.php",      type: "POST",      processData: false,      contentType: false,      data: fd,      success: function(d) {        console.log(d);      }    });  });</script></body></html>

總結:以上就是本篇文的全部內容,希望能對大家的學習有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.