3

ajax を使用して自分のサイトに .txt ファイルをアップロードできましたが、自分のサイトに .txt ファイルの内容も表示したいと考えています。これを行う方法がわかりません。ありがとう!

ここに私の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>

ここに私の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);
}
}); 

})();       



});  

ここに私の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)";
}
?>
4

2 に答える 2

3

ajaxリクエスト内で送り返すことができます:

ファイル-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):\n";
            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):\n";
            echo file_get_contents('/path/where/you/put/that/file.txt');
      }
   }
}
于 2013-10-15T17:03:16.990 に答える
-1
var fileText = load('file_name.txt');

ここに行くと、より多くの解決策を見つけることができます: http://forum.jquery.com/topic/reading-values-from-text-file

于 2013-10-15T17:17:04.817 に答える