0

I'd like to upload an image using Angular and Laravel. It's seems pretty easy but it's not. I copy my code it speaks for itself:

html:

<input type="file" name="image" id="image" custom-on-change="uploadFile">

Angular:

var f = document.getElementById('image').files[0];
var fd = new FormData();
fd.append("image",f);

Question.post({
    'name': this.name,
    'parent': this.parent,
    'image' : fd
  }).then(function () {
    $prevstate.go()
  }, function (response) {
    let alert = { type: 'error', 'title': 'Error!', msg: response.data.message }
    $state.go($state.current, { alerts: alert })
  })

And in the controller I have tried every way I found on the net but I got null everytime instead of my image:

Input::hasFile('image') 
$request->hasFile('image')
$image = Input::file('image');
$image = ($request->file('image')

No matter what method I use always returns with false or null. I hope you will find what I'm doing wrong. Thanks

J.Doe
  • 147
  • 1
  • 14
  • Have you defined a route in laravel to post the form data? The `Question.post` has no `url` property. – Sajal Sep 20 '17 at 09:45
  • Yes I defined it. I have more data what I'm trying to save and all the other data work well. – J.Doe Sep 20 '17 at 09:50
  • Check the headers in the network tab for the API, that will show if you actually send the file and its probably within `image --> image` – Sajal Sep 20 '17 at 09:59
  • I guess I found the problem the append to FormData is not working. After append it's still empty. I don't know why so that's the next thing to figure out. – J.Doe Sep 20 '17 at 10:58
  • Did you get image details in angular controller? – Nikita Sep 20 '17 at 12:07
  • Yes I get it. When I check the request/parameters(10)/image there is an array(0). So I got the image in the angular controller the problem is the way I send it. – J.Doe Sep 20 '17 at 12:11

0 Answers0