2013-10-18 18 views
0

我正在使用JQuery表單插件上傳文件。我能夠將上傳的txt文件的內容發送給與表單插件關聯的jQuery,但我想發送整個文件。在PHP中回顯整個文件併發送給jquery

現在我在我的PHP中迴應變量$ c,這是發送給我的jQuery。我可以用$ file發送整個文件嗎?

$(document).ready(function() { 

    (function() { 

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

    $('#dataform').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(data, responseText, statusText, $form) { 
      var percentVal = '100%'; 
      bar.width(percentVal) 
      percent.html(percentVal); 

      $.post('/submit', {name: data}, function(data) { 
       $('body').append(data); 
       }); 

     }, 
    complete: function(xhr) { 
     status.html(xhr.responseText); 
    } 
    // 
    }); 
    return false; 
    })();  

}); 

回答

1

你只需要保持一件事記住,Ajax請求將讀取服務器發送的輸出,這就是爲什麼有這麼一句話:

<?php 

header('Access-Control-Allow-Origin: *'); 
header('Access-Control-Allow-Methods: POST, GET, OPTIONS'); 

foreach($_FILES as $file) { 
    $n = $file['name']; 
    $s = $file['size']; 
$t = $file["tmp_name"]; 
$c= file_get_contents($t); 


    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)"; 
    } 
//echo "<br>uploaded: $n ($s bytes)"; 
} 

echo $c; 

?> 
在從jQuery形式插件我的jQuery

然後只是決定要發送的內容,並以純文本或任何形式發送它,如果你想獲取文件,可能會有問題,因爲服務器會嘗試執行內容,如果它是php文件,但有一個簡單的黑客攻擊,將文件擴展名重命名爲txt和服務器將按原樣發送,這是您做出的決定。

1

發送文件的內容等同於發送文件。問題是,你爲什麼想要做你正在問的問題。如果你想在服務器端處理文件處理,那麼恐怕這只是實現它的方法。

相關問題