Upload files using DropzoneJS with other fields I am trying to I am trying to use DropzoneJS for file uploading image file in an existing Laravel 5 simple. The problem am facing is that the file never get uploaded when I click on submit.
Here is my code so far:
@extends ('layouts.default')
@section ('content')
<link rel="stylesheet" href="{{ asset('css/dropzone.min.css') }}">
<script src="{{ asset('js/dropzone.js') }}"></script>
<div class="page_container">
<div class="wrapper">
<form class="dropzone dz-clickable" id="dropzonetask" enctype="multipart/form-data" method="POST" action="{{ url('/upload) }}">
<input type="text" name="name" />
<div class="dz-message">
<center <h4>Drag Photos to Upload</h4>
<span>Or click to browse</span>
</center>
</div>
<!-- Token -->
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
<button id="uploadall" class="btn btn-default pull-right">Upload All</button>
</form>
</div>
</div>
@stop
And this controller action to "var_dump" the file:
public function post_upload(){
//...
$the_pictures = Input::file('file');
return dd($the_pictures); //this return null
##edit:
I just move the above post_upload() function to the route file to make it more simple
//...
Route::post('upload', function(){
//...
$the_pictures = Input::file('file');
return dd($the_pictures); //this return null
});
try
use Illuminate\Http\Request;
class MyClass
{
public function post_upload(Request $request)
{
//...
if ($request->hasFile('file')) {
$the_pictures = $request->file('file');
return dd($the_pictures);
}
return 'no files attached!';
}
}
pretty sure the file form name (if it is multiple files) will be an array so you will need to loop over it if so.
foreach ($the_pictures as $picture)
{
// upload current picture in array
}
@Kryptonit3 OK, dumb question maybe but, where to put that code?
in your controller or repository class that handles the post request for the form.
And there are no dumb questions. Everyone is at a different level of understanding. This community is here to help those at every level.
@Kryptonit3 It give me this error:
FatalErrorException in C:\Bitnami\nginxstack-1.6.2-1\nginx\html\L5\app\Http\Controllers\Future_RomzyController.php line 177: Call to undefined method Illuminate\Support\Facades\Request::hasFile()
run php artisan --version from your terminal and post the output.
and your full controller method wouldn't hurt.
Or add this to the use block at the top of your controller
use Request;
@4goodapp sorry i just realised was tired eyes when i looked will take a look now can you show your full controller
@nathanrobjohn (No problem :) I imagine )
I just edit my first post, check it please.
I used this before in an old Laravel 4 project, might be missing parts but you can take what you want from it. Was still learning when I wrote this :P those else's!
$file = Input::file('file');
$destinationPath = 'uploads';
$file_label = Input::get('file_label');
$original_filename = $file->getClientOriginalName();
$extension = $file->getClientOriginalExtension();
$filename = str_random(12) . '-' . Str::slug(Str::limit($file->getClientOriginalName(), 50), '-') . '.' . $extension;
$filesize = $file->getSize();
$dimensions = getimagesize($file);
if ($dimensions > 0)
{
$file_type = 'image';
$dimensions = $dimensions[0] . ',' . $dimensions[1];
}
else
{
$file_type = 'file';
$dimensions = '';
}
if ($file_type == 'image')
{
$img = \Image::make(Input::file('file'));
$save_original_image = $img->save($destinationPath . '/' . $filename);
$do_upload = $img->resize(null, 400, function ($constraint)
{
$constraint->aspectRatio();
$constraint->upsize();
});
$save_image = $img->save($destinationPath . '/thumbnails/' . $filename);
if ($save_original_image)
{
if ( ! $save_image)
{
Response::json('Could not move file', 400);
}
}
else
{
Response::json('Could not save file', 400);
}
}
else
{
$do_upload = Input::file('file')->move($destinationPath, $filename);
}
if ($do_upload)
{
// Save the new image to DB
$media = new \Media();
$media->group_id = '1';
$media->label = $file_label;
$media->filename = $filename;
$media->filesize = $filesize;
$media->extension = $extension;
$media->dimensions = $dimensions;
$media->file_type = $file_type;
if ($media->save())
{
return Response::json('success', 200);
}
else
{
return Response::json('Error saving image to database', 400);
}
}
else
{
return Response::json('error', 400);
}
@bashy well this is similar to what I did before I use DropzoneJS, and everything was working as expected. Now I just want add a drag and drop fonctionality and as far as I know I have DropzoneJS and Jquery File Upload as options.
Yeah that was using DropzoneJS, it just does that method on each request.
@bashy OK, I think this is too much for me, can I just retrieve the file var_dump it it when I click on submit? just like I did on my Route file:
//...
Route::post('upload', function(){
//...
$the_pictures = Input::file('file');
return dd($the_pictures); // I just wanna
});
Sure? Basically DropzoneJS sends each image/file to a URL separately. You basically do a method for one image/file.
@bashy you're right, so is it possible to retrieve all images, assign them to an array then var dump it?
What are you trying to do? Are you trying to get all the images into one array to insert them into the database?
@bashy EXACTELY :) ... but just the "...all the images into one array..." part for the moment. I'll take care about the rest (like the validation and the use of a foreach loop for the database insertion )
Please sign in or create an account to participate in this conversation.