2

I'm having trouble with an XHR Request, for some reason my server is not receiving my files:

Here is my angular service update algorithm:

var update = function(id, name, file) {                   

    var formData = new FormData();

    formData.append('name', name);
    formData.append('img', file);  

    return $http({
        method              : 'PUT',
        url                 : '/albums/' + id,
        data                : formData,
        headers             : {'Content-Type': undefined},
        transformRequest    : angular.identity
    });

};

On my laravel controller I just have:

public function update($id) {

    return Response::json(Input::hasFile('img'));

}

The file is obviously there, why can't I retrieve it in my backend?

This is my request info:

Remote Address:[::1]:8000
Request URL:http://localhost:8000/albums/1
Request Method:PUT
Status Code:200 OK

Request Headers

Accept:application/json, text/plain, */*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8,es;q=0.6
Cache-Control:no-cache
Connection:keep-alive
Content-Length:13811
Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryJ46EVSBw57RaVu7x
Cookie:_token=eyJpdiI6IkkzSXVmdnhubFFlVnlzSnZNWVFzVWk3ZlVKSFRDNFFlNndJWUVsVGNVU2c9IiwidmFsdWUiOiI5OG5PamUrVGZkZGx0ajZONklWajJ2OTM3MWlRd2tGZ2g5S2Jja1RhVjJ4Q1wvYk9xQTB4TlRKUWxkWmdvRm1EcHlzTGRjSEdzN2U5TWNPYWxEYVExVUE9PSIsIm1hYyI6IjA4NTY0ZTlmMjAyNTk3NGQxMmFhODIxMTU3NGNiYjQ4ZDA3OTgxMTA3Yzk1MmVkNmJkMGNkYjUyMmNhMzZkNzQifQ%3D%3D; laravel_session=eyJpdiI6IjRISElnWjd3ZlwvY2k1Z1pvOERWOGxyVHlaQzEwRmlqY1FiV0tNNzZEbEs4PSIsInZhbHVlIjoiYnp4UzVqOFoxMm5MMXhQdzJhVFphSkgrRGh2b2plYXhjdXpTamJ0UjVYdGdxS0puQmpPVXhObEtyb1I3XC9HQnRFdnBMWXV0MzRmWXAybGRySGRvXC9vUT09IiwibWFjIjoiMGQ1NzUyYTBjZmU3NzQ3ZDBkYjg5ZWViOGZmYzg3ZDY1ODg0N2JmNDg1NmQyNmMwZDcxMDE5NzcxZjIxM2MxMiJ9
Host:localhost:8000
Origin:http://localhost:8000
Pragma:no-cache
Referer:http://localhost:8000/Admin/Client
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.104 Safari/537.36

Request Payload

------WebKitFormBoundaryJ46EVSBw57RaVu7x
Content-Disposition: form-data; name="name"

Some Weird Album
------WebKitFormBoundaryJ46EVSBw57RaVu7x
Content-Disposition: form-data; name="img"; filename="derpino.jpg"
Content-Type: image/jpeg


------WebKitFormBoundaryJ46EVSBw57RaVu7x--

Response Headers

Cache-Control:no-cache
Connection:close
Content-Type:application/json
Date:Wed, 29 Oct 2014 12:47:20 GMT
Host:localhost:8000
Set-Cookie:laravel_session=eyJpdiI6IlQ4WlFOaG1keVhXVlA1dlluNWFZZGlMcmRQNGM3bThCRjZ6cnh4ZlorcWs9IiwidmFsdWUiOiJOZkJXNXBJQTVSTGZzWHJ4alg1SXBoN0Q2ekR6UVpnWThKQ0c4MXZOQlc1RUhNMUUraUZSTlpPYTlPTFdLQXpiYTJONkRvb29WN1djVlZkSGdaWStjQT09IiwibWFjIjoiOWI5MzEwODE2YTZlM2EzODMwZDE1YzI4YmE4M2NiYWJjMTRjMDEzOGI3YjA4NmRlMGU5NDBlZWEyMzI4MGQ3MCJ9; path=/; httponly
X-Frame-Options:SAMEORIGIN
X-Powered-By:PHP/5.5.11
5
  • I have the exact same code on another controller an it works fine ... I don't get it Commented Oct 29, 2014 at 12:55
  • exact samecode in angular or laravel controller ? Commented Oct 29, 2014 at 12:59
  • I have another angular service, basically exactly like that, with some extra parameteres, and in my laravel controller I catch the file with Input::file(), and it works Commented Oct 29, 2014 at 13:04
  • in my other laravel controller Input::hasFile('img') return true Commented Oct 29, 2014 at 13:06
  • Can you please provide the HTML for the input, button, or whatever is initiating the update() function? Commented Jan 20, 2017 at 3:25

1 Answer 1

2

I found the error! Apparently I can not send a file with the PUT method, I changed the method to POST and It works. In both the service and the laravel route

var update = function(id, name, file) {                   

    var formData = new FormData();

    formData.append('name', name);
    formData.append('img', file);  

    return $http({
        method              : 'PUT',
        url                 : '/albums/' + id,
        data                : formData,
        headers             : {'Content-Type': undefined},
        transformRequest    : angular.identity
    });

};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.