【发布时间】:2021-10-04 08:50:11
【问题描述】:
需要在 laravel 中使用 ajax 保存文件图片 我用这个代码
控制器
public function store(Request $request)
{
$item = new Item;
// if remove $it_files_path , $it_files_name , $path code work correct
$it_files_path = $request->file('file');
$it_files_name = $it_files_path->getClientOriginalName();
$path = $request->file('file')->storeAs('uploads', $it_files_name, 'public');
$item->it_photo_name = $it_files_name;
$item->it_photo_path = $path;
$item->it_arabic_name = $request->it_arabic_name;
$item->it_english_name = $request->it_english_name;
$item->it_classification = $request->it_classification;
$item->it_weight_gm = $request->it_weight_gm;
$item->it_dim_length = $request->it_dim_length;
$item->it_dim_width = $request->it_dim_width;
$item->it_dim_hight = $request->it_dim_hight;
$item->save();
$item->id = $item->id;
return response()->json(['success' => true , 'item' => $item]);
}
如果我删除下面的注释行正常工作
查看
<div class="form-group">
<div class="custom-file col-md-12">
<label class="custom-file-label">إختر ملفات المنتج (صور-فيديو-وصف..الخ)</label>
<input type="file" name="file" class="custom-file-input" id="file">
<span class="text-danger" id="fileError"></span>
</div>
</div>
这是我的 ajax 代码
$.ajax({
url: 'add-new-item',
type: "POST",
data: $('#item,#shp_no_for_it').serialize(),
success: function(response) {
if (response.success){
$('#operation_id').val(response.item.id);
toastr.success('تم إضافة بيانات المنتج بنجاح');
} else {
toastr.error(response.errors.name);
}
}
});
这是我的路线代码
Route::post('add-new-item', [ItemController::class, 'store']);
我不知道我的代码有什么问题
【问题讨论】:
-
你的输入框在哪里?
-
编辑我的代码请检查
-
请分享您的
ajax请求和完整的代码 -
更新ajax代码
-
#item和#shp_no_for_it是什么,它们是表单、控件...?另见stackoverflow.com/questions/166221/…