Upload file with parameters in Silex with Angular-File-Upload

4

I'm doing a job with Angular (version 1.5.8) and using Silex as Web Service. To send the file to the Web Service I am using Angular-File-Upload (version 1.5.1), and my Angular service is like this:

self.salvarAnexo = function (id, descricao, anexo) {
    var deferred = $q.defer();
    var url = "http://127.0.0.1:8080/salvar-anexo";

    $upload.upload({
        url: url,
        data: {
            id: id,
            descricao: descricao,
        },

        file: anexo
    }).then(function (response) {

        deferred.resolve(response);

    }, function (error) {
        deferred.reject(error);
    });

    return deferred.promise;
};

And the Web Service looks like this:

    $this->controllers->post('/salvar-anexo', function (Application $app, Request $request) {

        $file = $request->files->get('file');

        if($file){
            $file->move(__DIR__ . '/../../../temp', $file->getClientOriginalName());
        }

        //$params = $request->request->all();

        var_dump($request->files);
        exit;

I usually get the file, I can move it to the location I need, but the parameters I'm sending in the data object I can not get in my Web Service.

Could anyone help me ??? Vlw Galera.

    
asked by anonymous 12.08.2016 / 01:02

3 answers

0

The solution I found was to fetch the data via the $ _POST environment variable, I did not think it was very good to be using a framework, but it worked.

    
31.08.2016 / 21:22
2

Angular code-file-upload. Omit the file attribute and add it to the object data .

$upload.upload({
    url: url,
    data: {
        id: id,
        descricao: descricao,
        file: anexo
    },
}).then(function (response) {
    deferred.resolve(response);
}, function (error) {
    deferred.reject(error);
});
    
29.11.2016 / 03:21
0

I do not know how the rest of your code is, but put a

echo $request->files 

and see if you get it.

    
30.08.2016 / 19:19