0

我的目标是将一些图像上传到服务器并为它们提供描述。

单击上传按钮时,这就是我想要发生的事情:

1)一个javascript函数动态添加一个表单来获取图像的描述。

2) 提交表格时:

 a) the description entered in the form must be available $_POST['description'] at server side.
 b) the images are sent to the server using an XMLHttpRequest

在我编写的代码中,描述不可用$_POST['description']。当我删除检查if(!isset($_POST['description']))时,图像文件已完美上传。这是我的代码:

javascript代码

upload.onclick = uploadPrompt;

// dynamically add a form
function uploadPrompt () {
    // fileQueue is an array containing all images that need to be uploaded
    if (fileQueue.length < 1) {
        alert("There are no images available for uploading.");
    } else {
        var inputDescription = document.createElement("input");
        inputDescription.className = "promptInput";
        inputDescription.type = "text";
        inputDescription.name = "description";

        var inputButton = document.createElement("button");
        inputButton.id = "promptInputButton";
        inputButton.type = "submit";
        inputButton.innerHTML = "Start uploading";

        var promptForm = document.createElement("form");
        promptForm.method = "post";
        promptForm.action = "upload.php";
        promptForm.onsubmit = uploadQueue;
        promptForm.id = "promptForm";
        promptForm.appendChild(inputDescription);
        promptForm.appendChild(inputButton);

        document.body.appendChild(promptForm);
    }
}

function uploadQueue(ev) {
    ev.preventDefault();

    elementToBeRemoved = document.getElementById("promptForm");
    elementToBeRemoved.parentElement.removeChild(elementToBeRemoved);

    while (fileQueue.length > 0) {
        var item = fileQueue.pop();
        // item.file is the actual image data
        uploadFile(item.file);
    }
}

function uploadFile (file) {
    if (file) {
        var xhr = new XMLHttpRequest();

        var fd = new FormData();
        fd.append('image',file);

        xhr.upload.addEventListener("error", function (ev) {
            console.log(ev);
        }, false);

        xhr.open("POST", "upload.php");

        xhr.setRequestHeader("Cache-Control", "no-cache");
        xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
        xhr.setRequestHeader("X-File-Name", file.name);

        xhr.send(fd);
    }
}

php代码上传.php

<?php

session_start();

if (!isset($_POST['description'])) {
    echo "upload:fail\n";
    echo "message:No scene was specified";
    exit();
}


if (isset($_FILES['image'])) {
    if(!move_uploaded_file($_FILES['image']['tmp_name'], "uploads/" . $_POST['description'] . "/" . $_FILES['image']['name'])) {
        echo "upload:fail\n";
        }
    else {
        echo "upload:succes\n";
    }
    exit();
}

exit();
?>
4

1 回答 1

0

当有大量开发人员已经更好地编写了相同的东西时,我真的建议不要创建自己的异步文件上传功能。查看这些选项:

我以前用过这两个,它们工作得很好。对于 BlueImp,您可以使用此选项发送额外的表单数据:

$('#fileupload').fileupload({
   formData: $('.some_form').serialize()
});

上面捕获了一个表单并序列化了它的输入。或者,您可以使用特定值(即来自 DOM 中的特定元素)填充数组或对象:

var array = new Array();
$('.description').each(function() {
   array[this.id] = this.value;
});

您将使用 ID 来链接您的文件和描述。

于 2013-02-16T11:04:44.593 回答