0

I'm using Vue + Axios and tried to submit file unto my Lumen app

HTML

<input type="file" ref="inputfile">
<input type="file" ref="inputfile2">
<button @click="submit">Submit</button>

Vue

methods : {
    submit(){
        var formData = new FormData();

        formData.append('listing_id',222);
        formData.append('listing_title','Listing Title');
        if( this.$refs.inputfile.value != '' ){
            formData.append('attachment',this.$refs.inputfile.files[0]);
        }
        if( this.$refs.inputfile2.value != '' ){
            formData.append('attachment2',this.$refs.inputfile2.files[0]);
        }


        axios.post(
            'https://api.domain.com/api/v3/claim',
            formData,
            {
                headers: {
                    'Content-Type': 'multipart/form-data'
                }
            }
        ).then(function(res){
            console.log(res);
        })

    }
}

and in my Lumen app

return response()->json([ 'success' => true, 'data' => $request->file('attachment') ]);

and the Lumen app just returns an empty object unexpectedly

enter image description here

I can confirm that there is a file request just by

$request->hasFile('attachment');

and it returns true, and also by

$request->file()

it returns me an array of empty objects.

I can validate also the formData contents by doing so

formData.forEach(function(el){
    console.log(el.name);
});

and it returns me the file name of each attachment (without extra data from above fields).

What seems wrong or problem that file requests returns an empty object instead?

halfer
  • 19,824
  • 17
  • 99
  • 186
Juliver Galleto
  • 8,831
  • 27
  • 86
  • 164
  • `file` will return an instance of [UploadedFile](https://github.com/symfony/symfony/blob/3.0/src/Symfony/Component/HttpFoundation/File/UploadedFile.php) What do you want to return exactly? – Clément Baconnier May 16 '19 at 06:28
  • @cbaconnier the problem is it simply showing an empty object {} so it returns undefined offset if I do $request->file()[0]->getClientOriginalName(); or return null if I do $request->file('attachment')->getClientOriginalName(); – Juliver Galleto May 16 '19 at 07:49
  • Your screenshot show `{success: false}` and you return a `{success: true}` Is this normal? Otherwise, Do you get something with `logger($request->file('attachment')->getClientSize())` or `getMimeType()` ? – Clément Baconnier May 16 '19 at 10:00
  • @cbaconnier dont mind the success, the column 'data' contains $request->file('attachment') – Juliver Galleto May 16 '19 at 10:09
  • @cbaconnier doing so $request->file('attachment')->getClientSize() returns null because the file request simply empty. I have no idea – Juliver Galleto May 16 '19 at 10:10
  • Did you try without the content type? https://stackoverflow.com/questions/42907747/how-to-send-a-file-via-axios-to-laravel – Clément Baconnier May 16 '19 at 10:13
  • @cbaconnier no luck, still empty. – Juliver Galleto May 17 '19 at 04:51
  • I can see you are returning `success => true`, yet in your response it is `success => false ` are you sure you get to the `return response()->json([ 'success' => true, 'data' => $request->file('attachment') ]);` – Nicolas Jul 19 '19 at 23:18

0 Answers0