0

I'm uploading files using laravel as backend API REST. When I upload a file in the frontend, it sends a 200 OK status code in the response, but doesn't save it to the database. Something weird that I see it's that in the response the data comes empty, and the array of the files comes in the config section of the response. 200 ok response

This is the controller that handles this situation

 public function makeFile(Request $request)
    {
        $fileArray = [];
    if (isset($request->files) && is_array($request->files)) {
        foreach ($request->files as $key => $fileEntity) {
            $file= new File();
            $file->file=$fileEntity['file'];
            $file->shipment_id=$fileEntity['shipment_id'];
            $file->user_id=$fileEntity['user_id'];
            $file->date=date('Y,m,d,G,i,s');
            $file->fileName=$fileEntity['fileName'];
            $file->fileType=$fileEntity['fileType'];
            $file->status=$fileEntity['status'];
            $file->save();

            $fileArray[] = $file;
        }
    }
    return response()->json($fileArray);
    }

This is my form

<uib-tab index="1" heading="Files">
                        <div class="row">
                        <label>Add File</label>
                            <ul class="shipment__files">
                                <li class="col-xs-12 col-md-6" ng-repeat="file in shipment.shipmentObject.files">
                                    <a href="#"><img ng-src="{{ file.iconURL }}" width="60"></a>
                                    <div class="filename"><a href="#">{{ file.fileName }}</a></div>
                                    <div class="date">{{ file.date }}</div>
                                    <div class="uploader">{{ file.user_id }}</div>
                                </li>
                            </ul>
                        </div>
                        <!-- <form>
                        <input type="file" name="" ng-model="shipment.shipmentObject.file">
                        <button class="btn btn-primary" ng-click="shipment.makeFile(shipmentObject.file)" >Upload File</button>

                        <div>aa {{ newFile }}</div>
                        </form> -->
                        <div>
                          <input data-my-Directive type="file" name="file" ng-model="shipment.shipmentObject.file">
                          <p>
                            post request with this data:
                          </p>
                          <pre ng-repeat="(key, value) in fileLog">
                            {{key}} {{ value }}
                          </pre>
                        </div>

                    </uib-tab>
Gabo Ruiz
  • 536
  • 4
  • 19

1 Answers1

0

So you're getting an empty response because your conditional statement isn't matching, so you're just returning that empty $fileArray you created:

if (isset($request->files) && is_array($request->files))

The correct way to check if there are files is to go:

if($request->hasFile('file'))

Everything you need to know about this is available in the official documentation here:

https://laravel.com/docs/5.4/requests#files

giolliano sulit
  • 996
  • 1
  • 6
  • 11
  • Im not getting an empty response, the response is 200 OK but its not saving it into the database. @giollianosulit – Gabo Ruiz Aug 02 '17 at 15:40
  • I don't think you understood my answer correctly. You're getting 200 because **youre returning an empty array**. The code in your if statement will never run. Check my solution above. – giolliano sulit Aug 02 '17 at 21:48
  • I understand, I put it like you mentioned. if($request->hasFile('file')) and added an dd("here") to debugg it, and I try it with postman. No "here" were printed. But when I change the if statment for this: if($request->files){ the dd"here" are printed. so that means the if statment its correctly. when i put the dd"here" inside the foreach, postman returns a "[]"... why is that? @giollianosulit – Gabo Ruiz Aug 02 '17 at 22:04
  • What is your file named? I posted the answer assuming you had one named, **file** – giolliano sulit Aug 02 '17 at 22:18
  • yas, its named file @giollianosulit – Gabo Ruiz Aug 02 '17 at 22:30
  • Did you remove the other check for isset? Can you post the full solution above – giolliano sulit Aug 02 '17 at 22:31
  • Yes I removed it like you suggest, I got no solution, im still playing with the controllers, both in frontend angularjs and in the backedn with laravel. @giollianosulit – Gabo Ruiz Aug 02 '17 at 22:58
  • Actually, not sure why I didn't see this at the start. Check out this answer: https://stackoverflow.com/questions/21044798/how-to-use-formdata-for-ajax-file-upload - is that how you're posting your Form? I think you'd need FormData – giolliano sulit Aug 02 '17 at 23:19
  • well, that answer doesn't work for me, because im not using ajax or jquery... @giollianosulit – Gabo Ruiz Aug 02 '17 at 23:36
  • Can you add your full form page to the initial question and also controller. That will help – giolliano sulit Aug 02 '17 at 23:38
  • I make a better question for this, based on what you ask me for. [here](https://stackoverflow.com/questions/45494929/uploading-file-from-angular-controller-to-laravel-api) @giollianosulit – Gabo Ruiz Aug 03 '17 at 21:44
  • And have you got the JS that posts the form ? – giolliano sulit Aug 03 '17 at 23:30