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

Asked

Viewed 327 times

4

I am doing a work 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 is 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 normally receive the file, I can move it to the location I need, but the parameters I am sending in the object date I am not being able to receive on my Web Service.

Could someone help me?? Vlw Galera.

3 answers

2

Angular-file-upload code. Omit attribute file and add 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);
});

0


The solution I found was to search the data via environment variable $_POST, I didn’t think it was right to be using a framework, but it worked.

0

I don’t know what the rest of your code looks like, but put a

echo $request->files 

and see if you get.

  • I tried to "debug" this way but for some reason the framework deletes any parameter that is passed along with the file

Browser other questions tagged

You are not signed in. Login or sign up in order to post.