Я пытаюсь загрузить файл из поля ввода. У меня есть следующие поля:
<tr>
<td>
<input id="item" class="form-control-file my-2 w-100" type="file">
</td>
<td>
<input id="remark" class="form-control my-2 w-100" type="text">
</td>
</tr>
Тогда мой топор ios post:
self.$http.post('http://127.0.0.1:8000/api/item/new', {
batchcode: document.getElementById("batchcode_form").value, // is somewhere else in the code, works fine.
product_code: document.getElementById("item").files[0].name,
remark: document.getElementById("item"),
input_type: 'item'
}).then(function (response) {
if (response.data.status === 'success') {
// success
} else {
// failed
}
});
В заголовках отображаются следующие данные:
batchcode "batchcode, который я заполнил"
input_type: "file"
product_code: "filenamewithoutpath.png"
примечание: "примечание, которое я заполнил"
Теперь у меня есть следующий код в моем itemscontroller:
public function store(Request $request)
{
if ($request->product_code != "" && $request->remark != "") {
// create item
$item = new Item;
$item->batchcode = $request->batchcode;
$item->remarks = $request->remark;
if ($request->input_type === "file") {
// upload file
$file = $request->product_code;
$item->product_code = $file->getClientOriginalName();
$new_name = time().$file->getClientOriginalName();
$path = public_path() . '/uploads/';
$file->move($path, $new_name);
$item->product_code = '/uploads/' . $new_name;
} else {
$item->product_code = $request->product_code;
}
$item->save();
return response()->json([
'status' => 'success',
'data' => $item
]);
} else {
return response()->json([
'status' => 'error',
'data' => 'Not all data was given.'
]);
}
}
При выполнении этого я получаю следующую ошибку: Call to a member function getClientOriginalName() on string
Я получаю правильное имя файла, но загрузка часть не работает. Я не могу понять или найти что-нибудь в Google, почему это не работает.