我正在尝试使用Ajax在laravel 5.3中上传图片。
我的刀片代码:
<form method="post" class="inline" enctype="multipart/form-data" >
<input style="margin-top:0px;" type="file" name="file" id="file" class="btn btn-block btn-primary"/>
<button type="button" class="btn btn-primary" data-toggle="modal" onclick="create1()">Create</button>
</form>
我的ajax电话:
function create1()
{
var photo = $("#file").val();
$.ajax({
url: '<?= URL:: to('store') ?>',
type: 'GET',
async : false,
data:
{
'photo':photo,
},
success: function(e)
{
if(e == 0)
{
alert("Success Full Created");
}
else
{
alert("Error");
}
}
});
}
路线呼叫:
Route::get('store','admin\ProductController@store');
控制器呼叫:
public function store(Request $request)
{
$post = $request->all();
$imageName = $file->getClientOriginalName();
$imagemove= $file->move(public_path('images'),$imageName);
$data123 = array ( "photo"=> $imagemove, );
$check222 = DB::table('product') -> insert($data123);
}
答案 0 :(得分:1)
使用当前的解决方案进行调试很困难,因为您正在使用AJAX请求上传文件。
您无法通过GET上传文件!
请执行以下操作:
type="submit"
Route::post('store','admin\ProductController@store');
<form method="post" class="inline" enctype="multipart/form-data" >
<input style="margin-top:0px;" type="file" name="file" id="file" class="btn btn-block btn-primary"/>
<input type="submit" class="btn btn-primary" value="Create">
</form>
Route::post('store','admin\ProductController@store');
如果您确定上传有效,请插入您的AJAX上传请求。
答案 1 :(得分:0)
一步一步我会跟着你
<form method="post" class="inline" enctype="multipart/form-data" >
<input style="margin-top:0px;" type="file" name="file" id="file" class="btn btn-block btn-primary"/>
<button type="button" class="btn btn-primary" data-toggle="modal" onclick="create1()">Create</button>
Ajax Call
function create1(){
var data = new FormData();
data.append('file', $('#file').get(0).files[0]);
$.ajax({
type:'post',
url:<?= URL:: to('store') ?>,
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
dataType:'HTML',
contentType: false,
processData: false,
data:data,
success:function(data){
if(e == 0)
{
alert("Success Full Created");
}
else
{
alert("Error");
}
}
});}
控制器: -
public function store(Request $request){
$post = $request->all();
$imageName = $file->getClientOriginalName();
$imagemove= $file->move(public_path('images'),$imageName);
$data123 = array ( "photo"=> $imagemove, );
$check222 = DB::table('product') -> insert($data123);
}