How to delete particular file form dropzone and database
This is my upload function:
public function postUpload(Request $request,$id)
{
$destinationPath = 'images';
$fileName= $request->file('file')->getClientOriginalName();
$upload_success = $request->file('file')->move($destinationPath, $fileName);
if($upload_success){
$image = new Image;
$image->filename = $fileName ;
$image->save();
}
}
This is my view js :
<script>
Dropzone.options.myAwesomeDropzone = {
maxFilesize: 2, // Size in MB
addRemoveLinks: true,
removedfile: function(file) {
var name = file.name;
console.log(file.name);
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
type: 'POST',
url: '/delete',
data: {filename:name},
dataType: 'html'
});
var fileRef;
return (fileRef = file.previewElement) != null ?
fileRef.parentNode.removeChild(file.previewElement) : void 0;
},
success: function(file, response) {
alert(response);
},
error: function(file, response) {
alert(response);
return false;
}
};
</script>
This works perfectly . when i click remove link in dropzone after uploading file it deletes image from database ,But my problem is if two images are of same name in db then one click deletes both image,which i don't want
obviously i can do like this $fileName= $request->file('file')->getClientOriginalName(); $newFileName=$fileName.time();
so filename may be unique in db.
but i need to delete file from database and through dropzone i get orginal file name
This is my delete method:
public function delete(Request $request){
$filename = $request->get('filename');//gives orginal file name eg:abc.jpg
Image::where('filename',$filename)->delete();
return "ok";
}