处理POST请求

时间:2014-03-31 18:07:46

标签: javascript php symfony web image-uploading

我正在使用javascript代码创建一个异步上传元素:

$("#file-input").change(function(){
    uploadImage(this.files[0]);
});

function uploadImage(imageFileHTMLInput) {
    var xml  = new XMLHttpRequest();
    var data = new FormData();
    data.append('file', cover);
    xml.open('POST', url);
    xml.send(data);
    xml.onreadystatechange = function() {
        if(xml.readyState === 4) {
            if(xml.status === 200) {
                var response = JSON.parse(xml.responseText);
                // handle response
            } else {
                var error = JSON.parse(xml.responseText);
                // handle error
            }
        }
    };
}

如何在Symfony2服务器中处理此帖子?我需要将此文件保存在服务器中并返回图像URL。

更新:

我对我的代码进行了一些更改,以便正常工作。我已经改变了上传的所有方式。

1 个答案:

答案 0 :(得分:1)

您将通过

从控制器中获取POST请求中的数据
$request->files->get('your_file_identifier_name');

服务器端,您将获得File

的实例
相关问题