2

I tried to upload a file via api to server.

function uploadUsing$http(file) {
    file.upload = Upload.http({
      url: 'api/upload' + $scope.getReqParams(),
      method: 'POST',
      headers: {
        'Content-Type': file.type
      },
      data: file
    });

    file.upload.then(function (response) {
      file.result = response.data;
    }, function (response) {
      if (response.status > 0)
        $scope.errorMsg = response.status + ': ' + response.data;
    });

    file.upload.progress(function (evt) {
      file.progress = Math.min(100, parseInt(100.0 * evt.loaded / evt.total));
    });
  }

If console.log(file), there is image data, but when i send the request to server i get an empty array.

server function:

 public function uploadAPI(Request $request)
    {
       $image = $request->file('file');
        dd($image);                         ---------> return []/null
        return response()->json('upload hit server');
    }

What's the problem here? Thanks!!

d3bug3r
  • 2,042
  • 3
  • 29
  • 64
  • 1
    I think you need to send your request encoded as multipart-form-data, maybe you could find help with this [post](http://stackoverflow.com/questions/13963022/angularjs-how-to-implement-a-simple-file-upload-with-multipart-form). – n00dl3 Oct 01 '15 at 10:02
  • In that case console log within `uploadUsing$http` wont return anything. But here it does. The problem is, from angular to api the `uploadAPI` not getting anything – d3bug3r Oct 01 '15 at 10:06
  • then try to `var_dump(file_get_contents("php://input"));` and tell me what is the result – n00dl3 Oct 01 '15 at 10:13
  • "In that case console log within uploadUsing$http wont return anything.". Trust me, it would return something... – n00dl3 Oct 01 '15 at 10:15
  • @JuniusRendel u mean var_dump the $request of the php function? – d3bug3r Oct 01 '15 at 10:18
  • I mean `var_dump(file_get_contents("php://input"));` I wanna know what you get via `POST`. It should show the raw POST input – n00dl3 Oct 01 '15 at 10:19
  • @JuniusRendel `var_dump($request)` return the files but empty ` public 'files' => object(Symfony\Component\HttpFoundation\FileBag)[43] protected 'parameters' => array (size=0) empty` – d3bug3r Oct 01 '15 at 10:20
  • SOOOOOOO.... this is a request encoding issue !!!!!!!! – n00dl3 Oct 01 '15 at 10:21
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/91075/discussion-between-junius-rendel-and-ngman). – n00dl3 Oct 01 '15 at 10:22

1 Answers1

0

Have you tried using

Upload.upload({
    url: 'upload/url',
    data: {file: file, 'param1': $scope.param1}
});

Instead?