我有表user
:
我使用dropzone.js
和laravel
上传图片。
为了将图像添加到数据库,它非常好,但我想在数据库中上传后删除图像。
当我点击Remove file
时,它会显示警报错误,如图所示
查看:
<form action="{{ url('/upload_image') }}" class="dropzone" id="my-dropzone">
{{ csrf_field( ) }}
</form>
<script>
Dropzone.options.myDropzone = {
addRemoveLinks: true,
init: function() {
this.on("removedfile", function(file) {
var name = file.name;
//alert(name);
$.ajax({
type: 'POST',
url: "{{action('UserController@deleteFile')}}",
data: {
image: name
},
success:function(data)
{
alert("success");
}
error: function(){
alert('error');
}
});
});
}
};
</script>
控制器:
public function upload_image(Request $request){
$user = new User();
$file = $request->file('file');
$custom_file_name = uniqid().$file->getClientOriginalName();
$path = $file->storeAs('public/',$custom_file_name);
$user->image = $custom_file_name;
$user->save();
return redirect('upload/create');
}
public function deleteFile(Request $request){
$title=$request->image;
$user = User::where('image','=',$title)->delete();
return ['success' => 'success'];
}