最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PHP+iframe模擬Ajax上傳文件功能示例

 更新時間:2019年07月02日 09:40:08   作者:webbc  
這篇文章主要介紹了PHP+iframe模擬Ajax上傳文件功能,結合實例形式分析了iframe模擬Ajax上傳文件與后臺php接收處理相關操作技巧,需要的朋友可以參考下

本文實例講述了PHP+iframe模擬Ajax上傳文件功能。分享給大家供大家參考,具體如下:

xmlhttprequest level 1中,Ajax是不能夠上傳文件的,因為js不能操作本地文件,但是市場上有一些Ajax異步上傳文件的插件,是怎么完成的呢?答案:可以使用iframe模擬Ajax上傳文件。接下來博主將使用iframe來模擬Ajax來上傳文件。

首先看一下效果圖:

文件結構圖:

09-iframe-upload.html文件:

頁面中有一個表單,表單中有一個上傳文件按鈕和提交按鈕,點擊提交按鈕執(zhí)行ajaxUpload函數(shù),然后動態(tài)創(chuàng)建iframe標簽,讓其不可見,最后設置表單的target屬性指向iframe。

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>iframe模擬Ajax上傳文件</title>
  <link rel="stylesheet" href="">
</head>
<script src="http://libs.baidu.com/jquery/1.7.2/jquery.min.js"></script>
<script>
  /**
   * 文件上傳
   * @return bool 是否提交表單
   * 1、捕捉表單提交的動作
   * 2、動態(tài)創(chuàng)建iframe標簽,然其不可見
   * 3、設置表單的target屬性指向iframe
   */
  function ajaxUpload(){
    var iframeName = 'upload'+Math.random();//給iframe取名
    $('<iframe name='+iframeName+' width="0" height="0" frameborder="0"></iframe>').appendTo($('body'));//動態(tài)創(chuàng)建iframe
    $('form:first').attr('target',iframeName);//設置form的target屬性
    $('#progress').html('<img src="progress.jpg"/>');//顯示上傳是否成功
    //return false;
  }
</script>
<body>
  <h1>iframe模擬Ajax上傳文件</h1>
  <h2 id="progress"></h2>
  <form action="09-iframe-upload.php" method="post" enctype="multipart/form-data" onsubmit="return ajaxUpload();">
    <p><input type="file" name="pic"/></p>
    <p><input type="submit" value="提交" /></p>
  </form>
</body>
</html>

09-iframe-upload.php文件:

首先延時3秒,為了能看到加載的圖片,然后判斷是否有上傳文件,然后返回一段Js代碼,這段js是在頁面中顯示是否上傳成功

<?php
/**
 * iframe模擬Ajax上傳文件
 * @author webbc
 */
sleep(3);//延時3秒
if(empty($_FILES)){
  echo 'no file';
}
$error = $_FILES['pic']['error'] == 0?'succ':'fail';//判斷上傳是否成功
echo "<script>parent.document.getElementById('progress').innerHTML='$error'</script>";//顯示上傳是否成功
?>

更多關于PHP相關內容感興趣的讀者可查看本站專題:《php文件操作總結》、《PHP目錄操作技巧匯總》、《PHP常用遍歷算法與技巧總結》、《PHP數(shù)據(jù)結構與算法教程》、《php程序設計算法總結》及《PHP網(wǎng)絡編程技巧總結

希望本文所述對大家PHP程序設計有所幫助。

相關文章

最新評論

安福县| 成武县| 新竹市| 岚皋县| 茂名市| 贵定县| 肥乡县| 乐清市| 庆城县| 甘孜| 天峻县| 天峻县| 滨州市| 濮阳市| 于都县| 灵川县| 开原市| 柘荣县| 凤凰县| 盘山县| 辛集市| 迭部县| 周口市| 浪卡子县| 渝北区| 卓资县| 铜山县| 博客| 如东县| 额济纳旗| 竹北市| 聂拉木县| 威远县| 亚东县| 晋宁县| 独山县| 磐石市| 酒泉市| 汕尾市| 永城市| 前郭尔|