Skip to content

Instantly share code, notes, and snippets.

@talha08
Created May 30, 2016 19:45
Show Gist options
  • Save talha08/234f1fa3fc41579e2738556c05c172da to your computer and use it in GitHub Desktop.
Save talha08/234f1fa3fc41579e2738556c05c172da to your computer and use it in GitHub Desktop.
........................................................
Route:
........................................................
Route::get('events/file-upload', array('as' => 'event.eventFileUpload', 'uses' => 'EventController@fileUploadView'));
Route::post('dropzone/uploadFiles', array('as' => 'event.upload', 'uses' => 'EventController@fileUpload'));
........................................................
Function:
........................................................
//view method
public function fileUploadView(){
$events= Event::lists('event_title','id')->all();
return view('event.eventFileUpload',compact('events'))->with('title',"Upload file");
}
//store method
public function fileUpload() {
$input = \Input::all();
$rules = array(
'file' => 'image|max:3000',
);
$validation = \Validator::make($input, $rules);
if ($validation->fails()) {
return \Response::make($validation->errors->first(), 400);
}
$destinationPath = 'upload/eventFile'; // upload path
$extension = \Input::file('file')->getClientOriginalExtension(); // getting file extension
$fileName = rand(11111, 99999) . '.' . $extension; // renameing image
$upload_success = \Input::file('file')->move($destinationPath, $fileName); // uploading file to given path
if ($upload_success) {
return \Response::json('success', 200);
} else {
return \Response::json('error', 400);
}
}
........................................................
View:
........................................................
<!DOCTYPE html>
<html>
<head>
<link href="https://cdnjs.cloudflare.com/ajax/libs/dropzone/4.0.1/min/dropzone.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="dropzone" id="dropzoneFileUpload">
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/4.0.1/dropzone.js"></script>
<script type="text/javascript">
var baseUrl = "{{ url('/') }}";
var token = "{{ Session::getToken() }}";
Dropzone.autoDiscover = false;
var myDropzone = new Dropzone("div#dropzoneFileUpload", {
url: baseUrl + "/dropzone/uploadFiles",
params: {
_token: token
}
});
Dropzone.options.myAwesomeDropzone = {
paramName: "file", // The name that will be used to transfer the file
maxFilesize: 2, // MB
addRemoveLinks: true,
accept: function(file, done) {
},
};
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment