使用blade使用AJAX上传图像
我试图在Laravel5中使用ajax上传图像。我正在使用以下代码,但什么也没有发生: image.blade.php使用blade使用AJAX上传图像,ajax,image,laravel,upload,blade,Ajax,Image,Laravel,Upload,Blade,我试图在Laravel5中使用ajax上传图像。我正在使用以下代码,但什么也没有发生: image.blade.php {{Form::open(array('action' => 'ImageController@store','files'=>true,'id'=>'upload_form'))}} Title: {{Form::text('title')}} Image: {{Form::file('image')}} {{Form::submit('s
{{Form::open(array('action' => 'ImageController@store','files'=>true,'id'=>'upload_form'))}}
Title: {{Form::text('title')}}
Image: {{Form::file('image')}}
{{Form::submit('submit',['id' => 'btnAddProduct'])}}
{{Form::close()}}
@include('flash::message')
ImageController.php
public function store(Request $request)
{
$destinationpath = public_path() . '/img/';
$image=$request->input('image');
$filename=$request->file('image')->getClientOriginalName();
$request->file('image')->move( $destinationpath,$filename );
return Response::json(['success' => true, 'file' => asset($destinationpath.$filename)]);
}
Ajax代码:
<script>
$(document).ready(function() {
$('#submitButtonId').on('click', function() {
$.ajsubmitButtonIdax({
url:'/imgC/store',
data:new FormData($("#upload_form")[0]),
dataType:'json',
async:false,
type:'post',
processData: false,
contentType: false,
success:function(response){
console.log(response);
},
});
});
});
</script>
我做错了什么?您有什么错误吗?什么不起作用?它只是用以下命令将我引导到页面:{“success”:true,“file:“http:\/\/localhost:8000\/C:\\wamp\\www\\laravel\\work\\public\/img\/4d26e7d0eaf822d4790bf9b793ac58dc.jpg”}数据库中没有保存任何内容,但
store
方法中没有保存数据库中任何内容的代码。您正在将上载的文件移动到公共/img
。我应该输入什么代码?为了使用Ajax上传,最简单的方法是为图像
表设置一个文件夹,这样您就可以轻松地将标题和文件路径存储在数据库中。
Route::resource('imgC', 'ImageController');