jeudi 3 août 2017

Uploading file from Angular controller to Laravel API

I've been searching a lot in the web and StackOverflow, about the right and easiest way for uploading a file through an AngularJS controller to a laravel controller, get the file, the fileName and the fileType and register it to the database. My way of doing it its not working. At making the request, the browser shows a 200 OK POST, and everything looks good, but it doesn't save it to the database.

AngularJS Controller

    vm.makeFile = function(newFile){
        console.log(newFile);
        vm.shipmentObject.files=[];
        vm.newDateFile= new Date();
        vm.newDateFile = $filter('date')(vm.newDateFile, "yyyy-MM-dd h:mm:ss");
        vm.name="FILE" + vm.newDateFile
        vm.shipmentObject.files.push({
            // "id":77,
            "shipment_id":43,
            "user_id":20,
            "date":vm.newDateFile,
            "file": "vm.shipmentObject.file",
            "fileName":"aguacate",
            "fileType":"jpg",
            "status":"I"
        });



        console.log("Antes del resource",vm.shipmentObject.files);
        ShipmentResource.insertFile(vm.shipmentObject.files)
        .then(function(response){
            console.log("response",response);
            console.log("FILE INSERTED CORRECTLY");
            console.log("responseconfigdata",response.config.data);
            console.log("shipmentobjectfile",vm.shipmentObject.files);
        }, function(err){
            console.log(err);
        });
        vm.shipmentObject.newFile="";

            console.log(vm.shipmentObject.files);
    };

Resource that handles the http request

ShipmentResource.insertFile = function(file){
    return $http.post(WS.BaseURL + '/shipment/file/', file);
};

Laravel Controller

 public function makeFile(Request $request)
    {

        $fileArray = [];
         if($request->files){
            // dd($request->files);
        foreach($request->files as $key => $fileEntity){
// dd("go");
            $file = new File();
            $file->file=$fileEntity['file'];
            $file->shipment()->associate($shipment);
            $file->date=date('Y,m,d,G,i,s');
            $file->user_id=$fileEntity['user_id'];
            $file->fileType=$fileEntity['fileType'];
            $file->status=$fileEntity['status'];
            $name = 'zignyWS' . time() . '.' . $files->getClientOriginalExtension();
            $file->fileName=$name;  
            $path = public_path() . '/uploads/invoices/';
            $files->move($path,$name);
            $file->file='invoices/'.$name;
            $file->save();
        }
    }

    return response()->json($fileArray);
    }

I have this in the file model File.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class File extends Model
{
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'w8_w8file';

    public $timestamps = false;


    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    //protected $fillable = ['name', 'email', 'password'];

     protected $fillable = ['id', 'shipment_id', 'user_id', 'date', 'file', 'fileName', 'fileType', 'status'];   

    protected $casts = [
        'shipment->files' => 'array'
    ];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    //protected $hidden = ['password', 'remember_token'];


    public function shipment()
    {
        return $this->belongsTo('App\Shipment','shipment_id');
    }
}

This is the Route in Laravel

Route::post('shipment/file','ShipmentController@makeFile');

Please consider a way of not adding nothing extra in the frameworks



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2wpT3wE
via IFTTT

Aucun commentaire:

Enregistrer un commentaire