dongre1907 2013-10-15 16:59
浏览 101
已采纳

显示用ajax上传的txt文件的内容

I've been able to get upload a .txt file to my site using ajax, but I also want to display the contents of the .txt file on my site. I'm not sure how to go about doing this. Thanks!

here is my html

<!DOCTYPE html>
<html>
    <head> 
            <script type="text/javascript" src="static/jquery-1.4.2.js"></script>
            <script type="text/javascript" src="static/jquery-ui-1.8.4.custom.min.js"></script>
            <script type='text/javascript' src='static/js_test.js'></script>
            <script type='text/javascript' src='static/jquery.form.js'></script> 

            <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
            <script src="http://malsup.github.com/jquery.form.js"></script>
    </head>
    <body>    
            <form action="http://malsup.com/jquery/form/file-echo2.php" method="post" enctype="multipart/form-data">
                <input type="file" name="myfile"/>
                <input type="submit" value="Submit File"/>
            </form>



            <div class="progress">
                <div class="percent">0%</div >
            </div>

            <div id="status"></div>
    </body>
</html>

here is my jquery

$(document).ready(function() {

(function() {

var bar = $('.bar');
var percent = $('.percent');
var status = $('#status');

$('form').ajaxForm({
    beforeSend: function() {
        status.empty();
        var percentVal = '0%';
        bar.width(percentVal)
        percent.html(percentVal);
    },
    uploadProgress: function(event, position, total, percentComplete) {
        var percentVal = percentComplete + '%';
        bar.width(percentVal)
        percent.html(percentVal);
    },
    success: function() {
        var percentVal = '100%';
        bar.width(percentVal)
        percent.html(percentVal);
    },
complete: function(xhr) {
    status.html(xhr.responseText);
}
}); 

})();       



});  

here is my php

<?php
foreach($_FILES as $file) {
   $n = $file['name'];
    $s = $file['size'];
   if (is_array($n)) {
      $c = count($n);
      for ($i=0; $i < $c; $i++) {
         echo "<br>uploaded: " . $n[$i] . " (" . $s[$i] . " bytes)";
      }
   }
   else
      echo "<br>uploaded: $n ($s bytes)";
}
?>
  • 写回答

2条回答 默认 最新

  • dongshuzhuo5659 2013-10-15 17:03
    关注

    You can just send it back within the ajax request:

    in file-echo2.php:

    <?php
    
    foreach($_FILES as $file) {
       $n = $file['name'];
       $s = $file['size'];
    
       if (is_array($n)) {
          $c = count($n);
          for ($i=0; $i < $c; $i++) {
             if (move_uploaded_file($n[$i], '/path/where/you/put/that/file.txt')) {
                echo "uploaded " . $n[$i] . " (" . $s[$i] . " bytes):
    ";
                echo file_get_contents('/path/where/you/put/that/file.txt');
             }
          }
       }
       else {
          if (move_uploaded_file($n, '/path/where/you/put/that/file.txt')) {
                echo "uploaded " . $n . " (" . $s . " bytes):
    ";
                echo file_get_contents('/path/where/you/put/that/file.txt');
          }
       }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧