【问题标题】:upload with ajax and php without iFrame or flash engine使用 ajax 和 php 上传,无需 iFrame 或 flash 引擎
【发布时间】:2012-04-15 20:51:22
【问题描述】:

我正在尝试制作一个带有进度条的 ajax 上传功能,我试图简化我在互联网上找到的一个示例,我来到了这个功能:

<form>
        <input type="file" id="file" /><label for="file"></label><label id="aaa"></label>
        <button id="sub">abc</button>
    </form>

    <script src="js/jquery-1.7.1.min.js"></script>
    <script>

    function uploadFile(files) {
        var xmlhttp;
        if(window.XMLHttpRequest)
            xmlhttp = new XMLHttpRequest();
        else
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

        xmlhttp.upload.onprogress = function(e) {
            $("#aaa").empty().append(e.loaded + " - " + e.total);
        }

        xmlhttp.upload.onload = function(e) {
            $("#aaa").empty().append("finish");
        }

        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                alert(xmlhttp.responseText);
            }
        }

        xmlhttp.open("post", "post.php", true);
        xmlhttp.setRequestHeader("If-Modified-Since", "Mon, 26 Jul 1997 05:00:00 GMT");
        xmlhttp.setRequestHeader("Cache-Control", "no-cache");
        xmlhttp.setRequestHeader("X-Requested-With", "XMLHttpRequest");
        xmlhttp.setRequestHeader("X-File-Name", files[0].fileName);
        xmlhttp.setRequestHeader("X-File-Size", files[0].fileSize);
        xmlhttp.setRequestHeader("Content-Type", "multipart/form-data");
        xmlhttp.send(files[0]);
    }

    $(document).ready(function() {

        $("#file").change(function() {
            uploadFile(this.files);
        });

    });

    </script>

当我上传接近或低于 800kb - 1MB 的文件时它运行良好,而且速度非常快,但当我尝试上传更大的文件时它会出错。进度条显示正在上传,但onreadystatechange没有触发,文件也没有出现在服务器上。

回复这个ajax的php是这样的:

<?php
// e.g. url:"page.php?upload=true" as handler property
//print_r($_SERVER);
if(
    // basic checks
    isset(
        $_SERVER['CONTENT_TYPE'],
        $_SERVER['CONTENT_LENGTH'],
        $_SERVER['HTTP_X_FILE_NAME']
    ) &&
    $_SERVER['CONTENT_TYPE'] == 'multipart/form-data'
){
    // create the object and assign property
    /*$file = new stdClass;
    $file->name = basename($_SERVER['HTTP_X_FILE_NAME']);
    $file->size = $_SERVER['HTTP_X_FILE_SIZE'];
    $file->content = file_get_contents("php://input");

    // if everything is ok, save the file somewhere
    if(file_put_contents('files/'.$file->name, $file->content))
        echo "OK";*/
        $file->name = basename($_SERVER['HTTP_X_FILE_NAME']);
        $input = fopen('php://input', 'rb');
$file = fopen('files/'.$file->name, 'wb');
stream_copy_to_stream($input, $file);
fclose($input);
fclose($file);
} else {
    // if there is an error this will be the output instead of "OK"
    echo "Error";
}
?>

提前谢谢你,丹尼尔!

【问题讨论】:

    标签: php ajax file upload progress


    【解决方案1】:

    查看服务器变量:

    • MAX_FILE_SIZE
    • upload_max_filesize
    • post_max_size
    • memory_limit

    http://php.net/manual/en/features.file-upload.post-method.php

    您可能对这些变量有一些限制。

    问候!

    【讨论】:

    • 有趣的是,例如,当我尝试上传 JPG 时 .. 大约 500 kb,有时它可以工作,有时它不会,它只是阻塞并且不会触发 onreadystatechange
    • 检查变量 $_file["your_upload_file"] ["error"] 的值,它是否包含除零以外的任何错误代码?执行 var_dump($_FILE) 查看要上传的文件信息。
    • 当我执行 var_dump($_FILES) 时,它只显示数组.. 如果我尝试使用 $_file["your_upload_file"] ["error"] 它不起作用,因为我在提交时不上传,我尝试在输入上上传 onChange
    • 我的意思是在您的 post.php 文件中执行 var_dump($_FILES) 或 print_r($_FILES)。
    • 好像它没有在表单上发送文件。也许不能使用值为“multipart/form-data”的参数“enctype”?我不知道还能尝试什么。
    猜你喜欢
    • 2012-04-16
    • 2023-03-18
    • 2011-04-05
    • 1970-01-01
    • 2012-12-01
    • 1970-01-01
    • 2019-03-23
    • 1970-01-01
    • 2018-11-20
    相关资源
    最近更新 更多