0

xhr.upload.addEventListener("error", failed, false) 与我有什么不同$success = move_uploaded_file($tmp_name, $name); if $(success) { echo "0" } else { echo "1" }?这是不必要的冗余吗?

function uploadPHP() {
    var xhr = new XMLHttpRequest();

    xhr.upload.addEventListener("progress", updateProgress, false);
    xhr.upload.addEventListener("load", complete, false);
    xhr.upload.addEventListener("error", failed, false);
    xhr.upload.addEventListener("abort", cancelled, false);

    var foo = document.getElementById("uploadScreen");
    var form = document.getElementById("uploadForm");
    var percentComplete;
    var index;

    xhr.onreadystatechange = function () {
        if (xhr.readyState == 1){
        }
--

    function cancelled() {
        //cancel
    }
}

localhost file_server # cat php/upload.php

<?php
//
//require_once('PhpConsole.php');
//PhpConsole::start();
$tmp_name = $_FILES['file1']['tmp_name'];
$path = "/trunk";
$name = $path . DIRECTORY_SEPARATOR . $_FILES['file1']['name'];
$success = move_uploaded_file($tmp_name, $name);
if ($success) {
echo "0";
} else {
    echo "1";
}
?>
4

1 回答 1

2

"error"如果您的请求未通过,例如超时,则会发生。

假设您需要验证一些数据。如果它无效,"error"除非您在 PHP 中抛出未捕获的异常,否则该事件不会发生。相反,它只会返回一些指示值无效的数据,例如 string "0"

在您给定的示例中,完全有可能move_uploaded_file失败,但错误处理程序不会触发,因为从技术上讲,请求已完成而没有意外事件。您想要采取的行动失败的唯一迹象将是"0"响应。

于 2012-11-18T18:59:11.643 回答