Laravel 5.3 文件上传 调用成员函数 getClientOriginalName() on string
Laravel 5.3 File Upload Call to a member function getClientOriginalName() on string
我正在尝试使用 Ajax 在 laravel 5.3 中上传图片。
我的blade代码:
<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);
}
很难用您当前的解决方案进行调试,因为您正在使用 AJAX 请求上传您的文件。
您不能通过 GET 上传文件!
所以请执行以下操作:
- 禁用 AJAX 请求
- 确保您的提交按钮具有
type="submit"
- 将您的路线更改为
Route::post('store','admin\ProductController@store');
- 测试上传
您的 html 表格应该是:
<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>
您的 routes.php 或路线文件:
Route::post('store','admin\ProductController@store');
好的...?
如果您确定上传有效,请插入您的 AJAX 上传请求。
一步一步跟着你
<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 通话
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);
}
我正在尝试使用 Ajax 在 laravel 5.3 中上传图片。
我的blade代码:
<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);
}
很难用您当前的解决方案进行调试,因为您正在使用 AJAX 请求上传您的文件。
您不能通过 GET 上传文件!
所以请执行以下操作:
- 禁用 AJAX 请求
- 确保您的提交按钮具有
type="submit"
- 将您的路线更改为
Route::post('store','admin\ProductController@store');
- 测试上传
您的 html 表格应该是:
<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>
您的 routes.php 或路线文件:
Route::post('store','admin\ProductController@store');
好的...?
如果您确定上传有效,请插入您的 AJAX 上传请求。
一步一步跟着你
<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 通话
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);
}