【问题标题】:echo entire file in php and send to jquery在 php 中回显整个文件并发送到 jquery
【发布时间】:2013-10-18 18:35:58
【问题描述】:

我正在使用 JQuery 表单插件来上传文件。我可以将上传的 txt 文件的内容发送到与表单插件关联的 jquery,但我想发送整个文件。

现在我在我的 php 中回显变量 $c 并将其发送到我的 jquery。我可以用 $file 以某种方式发送整个文件吗?

<?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 表单插件中:

$(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;
    })();       

});  

【问题讨论】:

    标签: php jquery jquery-plugins jquery-forms-plugin


    【解决方案1】:

    您只需要记住一件事,ajax 请求将读取服务器发送的输出,这就是为什么它只读取echoed 语句的原因,只需决定您要发送的内容并将其作为纯文本发送或任何您喜欢的形式,如果您想获取文件,可能会出现问题,因为如果它是php 文件,服务器将尝试执行内容,但是有一个简单的破解,将文件的扩展名重命名为txt 之类的东西,服务器会按原样发送,这是您的决定。

    【讨论】:

      【解决方案2】:

      发送文件内容相当于发送文件。问题是,你为什么要做你所要求的。如果您想在服务器端节省文件处理,那么恐怕这就是这样做的方式。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-10-17
        • 2014-01-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-13
        • 1970-01-01
        • 2013-03-13
        相关资源
        最近更新 更多