Bribin's avatar

DropzoneJs files save order as filename to database laravel 5

I am working on a project which involves file uploading. I am using DropzoneJS and Laravel for this project. Everything seems to work fine, i want to save files with order.i want to pass the image order and save image file in that order.

var baseUrl = "{{ url('/') }}";
var token = "{{ Session::getToken() }}";

Dropzone.autoDiscover = false;
var myDropzone = new Dropzone(".dropzone", {
    url: baseUrl + "/upload",
    params: {
        _token: token

    }
});
Dropzone.options.myAwesomeDropzone = {
    paramName: "flyers",
    acceptedFiles: "image/*",
    enqueueForUpload: true,
    forceFallback: false,
    dictDefaultMessage: "Drop files here to upload",
    autoProcessQueue: false,
    uploadMultiple: false,
    maxFilesize: 2, // MB
    addRemoveLinks: true,

    accept: function(file, done) {

    },
};
    

     public function album()
    {
    $file = Input::file('file');
    $destinationPath = 'uploads';
    $filename = $file->getClientOriginalName();
    $uploadSuccsess = Input::file('file')->move($destinationPath, $filename);
     if( $uploadSuccsess ) {
         return Response::json('success', 200);
    } else {
      return Response::json('error', 400);
      }
}
0 likes
7 replies
sid405's avatar

@bribin Are you intending to upload multiple images for the same order? If so do you have a model for these images?

Bribin's avatar

@sid405 yes. I want to Upload multiple images in the same order. and after uploading also i want to sort the order

sid405's avatar

@bribin

  1. Can i see that?
  2. Are you attaching these images to any other model?
Bribin's avatar

@sid405 I have flyers and photos. Each flyer contains many photos.

namespace App;

use Image; use Illuminate\Database\Eloquent\Model; use Symfony\Component\HttpFoundation\File\UploadedFile;

class Photo extends Model {

/**
 * Database table
 *
 * @var string
 */
protected $table = 'flyer_photos';

/**
 * Fillable fields
 *
 * @var array
 */
protected $fillable = [
    'path',
    'name',
    'thumbnail_path'
];

/**
 * Base path to images
 *
 * @var string
 */
protected $baseDir = 'images/home_pics';


/**
 * Get instance from form
 *
 * @param $name
 * @return static
 * @internal param UploadedFile $file
 */
public static function named($name)
{

    return (new static)->saveAs($name);

}

/**
 * Set values to this
 *
 * @param $name
 * @return $this
 */
public function saveAs($name)
{

    $this->name = sprintf("%s-%s", time(), $name);
    $this->path =sprintf("%s/%s", $this->baseDir, $this->name);
    $this->thumbnail_path = sprintf("%s/tn-%s", $this->baseDir, $this->name);

    return $this;

}

/**
 * Move photo
 *
 * @param UploadedFile $file
 * @return $this
 */
public function move(UploadedFile $file)
{

    $file->move($this->baseDir, $this->name);

   $this->makeThumbnail();

    return $this;

}

/**
 * Generate thumbnail
 *
 */
private function makeThumbnail()
{

    Image::make($this->path)
        ->fit(200)
        ->save($this->thumbnail_path);
}


/**
 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
 */
public function flyers()
{

    return $this->belongsTo('App\Flyer');

}

}

spodlogar's avatar

dropzone handles all the requests very fast so ordering them correctly might not be the greatest idea,

But if you save them with a created_at of uploaded_at field and ordered them by that it would be pretty close.

Please or to participate in this conversation.