使用ajax,jquery和PHP上传带有其他信息的Image

时间:2015-09-21 19:56:01

标签: javascript php jquery ajax file-upload

我在使用ajax + php上传图片时遇到问题。尝试添加dataResult.file = form_data时,我收到非法的调用消息;正如您在下面的代码中看到的那样。

我非常感谢任何帮助!我非常坚持这一点并且已经搜索了stackoverflow相当多的答案可以帮助我。

function uploadImage (upFile,userId,description,category) {
    console.log("We have reached the point where we connect to the service to upload the image...");
    var dataResult = {};
    var form_data = new FormData();
    form_data.append('file',upFile[0]);
    console.log("Is the information on the form data variable?");
    console.log(form_data);
    dataResult.file = form_data;
    dataResult.idUser = userId;
    dataResult.description = description; 
    dataResult.nameCat = category;

    console.log("Description " + dataResult.description);
    console.log("Id User " + dataResult.idUser);
    console.log("name Cat " + dataResult.nameCat);

    console.log("This is the information that is being sent thorugh ajax");
    console.log(dataResult);

    $.ajax({
            url: 'http://localhost/App/www/classes/actionsPhotos/uploadPhoto.php',
            type: 'post',
            data: dataResult,
            success: function (data) {
                console.log("All is good and working.");
                console.log(data);
            },
            error: function(errorInfo){
                console.log("There was an error: ");
                console.log(errorInfo);
            }
        });
}

1 个答案:

答案 0 :(得分:0)

好的我通过稍微调整代码来解决这个问题,这是针对遇到同样问题的任何人的新代码。

function uploadImage(upFile,userId,description,category){     console.log(“我们已经达到了连接服务以上传图像的程度......”);     var form_data = new FormData();     form_data.append( '文件',upFile [0]);     form_data.append( 'ID用户所',用户id);     form_data.append( '描述',说明);     form_data.append( 'nameCat',类);

$.ajax({
        url: 'http://localhost/App/www/classes/actionsPhotos/uploadPhoto.php',
        type: 'post',
        data: form_data,
        cache: false,
        processData: false,
        contentType: false,
        success: function (data) {
            console.log("All is good and working.");
            console.log(data);
        },
        error: function(errorInfo){
            console.log("There was an error: ");
            console.log(errorInfo);
        }
    });

}