使用AngularJS在对象内发送文件

时间:2016-03-29 18:06:58

标签: php angularjs

我有一张表格,可以与N名参与者一起注册项目。 我现在被要求添加一个每个参与者必须发送学校证书的字段 问题是,在对象中添加此字段,我不再能够将数据发送到PHP。

我必须发送的JSON格式如下:

{
    "description": [STRING],
    "name": [STRING],
    "objective": [STRING],
    "participants": [NUMBER],
    "regulament": [BOOLEAN],
    "resume": [STRING],
    "users": {
        "1": {
            "born": [DATE],
            "educationCertificate": [OBJECT], // File
            "email": [STRING],
            "genre": [BOOLEAN],
            "level": [NUMBER],
            "name": [STRING],
            "neighborhood": [STRING],
            "phone": [STRING],
            "school": [STRING],
            "socialNumber": [STRING]
        },
        . . .
        "n": {
            "born": [DATE],
            "educationCertificate": [OBJECT (FILE)],
            "email": [STRING],
            "genre": [BOOLEAN],
            "level": [NUMBER],
            "name": [STRING],
            "neighborhood": [STRING],
            "phone": [STRING],
            "school": [STRING],
            "socialNumber": [STRING]
        }
    }
}

要发送文件,我的$ http看起来像这样:

$http({
    method: "POST",
    url: [SCRIPT],
    headers: {
        "Content-Type": undefined
    },
    data: $scope.data,
    transformRequest: function (data) {
        var formData = new FormData();

        angular.forEach(data, function (value, key) {
            formData.append(key, value);
        });

        return formData;
    }
}).success(function(response) {
    // do something
});

在PHP中,这是我检索AngularJS发送的数据的函数:

function param() {
    $array = array();
    $request = new stdClass();

    if (count($_GET) || count($_POST) || count($_FILES)) {
        $request = json_decode(json_encode(array_merge($_GET, $_POST, $_FILES)), false);
    } else {
        $request = json_decode(file_get_contents("php://input"));
    }

    if ($request) {
        $array = array_filter(array_map(function($data) {
            return is_string($data) ? trim($data) : $data;
        }, get_object_vars($request)), function($data) {
            return is_string($data) ? strlen($data) : $data;
        });
    }

    return json_decode(json_encode($array), false);
}

/* In the script */
$request = param();

然而,PHP中的数据如下:

{
    "description": [STRING],
    "name": [STRING],
    "objective": [STRING],
    "participants": [NUMBER],
    "regulament": [BOOLEAN],
    "resume": [STRING],
    "users": [STRING] // "[object Object]"
}

我总是使用"Content-Type": undefined,但我尝试使用"Content-Type": "multipart/form-data""Content-Type": "application/x-www-form-urlencoded;"。但结果更糟。

有谁知道如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我最近遇到了与Google App Engine(Java)和Servelet相同的问题。我和你的情况一样。用户可以上传5个附件,我需要上传这些文件以及其他一些信息。在我的情况下$ http不起作用。我为每个文件附件做了个别请求。不是您问题的实际解决方案,但这肯定会对您有所帮助。

var formData = new FormData();
formData.append('attachment',FILE_OBJECT);  
$.ajax({
    url: "URL",
    type: "POST",
    data: formData,
    cache: false,
    contentType: false,
    processData: false,
    xhr: function() {  // Custom XMLHttpRequest
        var myXhr = $.ajaxSettings.xhr();
        return myXhr;
      },
    }).success(function( data ) {
        console.log("File uploaded successfully");
    }).error(function(data){
        console.log("Somwthing went wrong");
    });