【发布时间】:2026-01-12 12:50:02
【问题描述】:
我在使用 axios 的 vue js 上的图像更新功能有问题。我已成功使用图像创建数据,但现在我正在更新图像
这是模板代码部分
<template>
<q-page-container>
<transition
enter-active-class="animated fadeInLeft"
leave-active-class="animated fadeOutRight"
appear
:duration="700"
>
<q-page>
<q-toolbar elevated class="text-primary">
<q-btn
flat
round
dense
icon="arrow_back"
@click="$router.push('/produk')"
/>
<q-toolbar-title>
Edit produk
</q-toolbar-title>
</q-toolbar>
<form>
<div class="row justify-center">
<p style=" width:90%; margin-top:10px;">Nama barang :</p>
<q-input
name="namaproduk"
dense
outlined
v-model="dataproduk.namaproduk"
lazy-rules
:rules="[
val => (val && val.length > 0) || 'Please type something'
]"
label="Nama produk"
style="width:90%; margin-right:10px; margin-left:10px; margin-bottom: 20px; justify-content: center;"
/>
<p style=" width:90%; ">Harga barang :</p>
<q-input
name="harga"
dense
outlined
v-model="dataproduk.harga"
type="number"
label="Harga produk"
style="width:90%; margin-right:10px; margin-left:10px; margin-bottom: 20px; justify-content: center;"
lazy-rules
:rules="[
val => (val && val.length > 0) || 'Please type something'
]"
/>
<p style=" width:90%;">Deskripsi :</p>
<q-editor
v-if="dataproduk.deskripsi"
name="deskripsi"
style="width:90%; margin-right:10px; margin-left:10px; margin-bottom: 20px; justify-content: center;"
v-model="dataproduk.deskripsi"
:definitions="{
bold: { label: 'Bold', icon: null, tip: 'My bold tooltip' }
}"
/>
<p style=" width:90%;">Gambar sekarang :</p>
<img
v-if="dataproduk.gambar"
:src="'http://127.0.0.1:8000/storage/' + dataproduk.gambar"
style="width:150px; height:150px;"
/>
<p style=" width:90%;">Ubah Gambar :</p>
<q-file
name="gambar"
@change="handleFileObject()"
dense
outlined
v-model="image"
label="Gambar produk"
style="width:90%; margin-right:10px; margin-left:10px; margin-bottom: 20px; "
/>
<q-btn
@click.prevent="ubah"
type="submit"
dense
color="primary"
label="Edit"
style="width:90%; margin: 20px; justify-content: center;"
/>
</div>
</form>
</q-page>
</transition>
</q-page-container>
</template>
该方法用于修改数据
methods: {
handleFileObject() {
this.image = this.$refs.file.files[0];
},
ubah() {
var dataproduk = new FormData();
dataproduk.append("namaproduk", this.dataproduk.namaproduk);
dataproduk.append("harga", this.dataproduk.harga);
dataproduk.append("deskripsi", this.dataproduk.deskripsi);
dataproduk.append("gambar", this.image);
_.each(this.dataproduk, (value, key) => {
dataproduk.append(key, value);
});
axios
.put(
"http://127.0.0.1:8000/api/produk/update/" + this.id,
this.dataproduk,
{
headers: {
"Content-Type":
"multipart/form-data; charset=utf-8; boundary=" +
Math.random()
.toString()
.substr(2)
}
}
)
.then(response => this.$router.push("/indexadmin"))
.catch(err => {
if (err.response.status === 422) {
this.errors = [];
_.each(err.response.data.errors, error => {
_.each(error, e => {
this.errors.push(e);
});
});
}
});
}
如果我不使用字符串和整数形式的“content-type:multipart/form-data”数据,它会成功更改。如果我使用“content-type:multipart/form-data”,那么所有数据都无法更新
添加“content-type:multipart/form-data”时出错
异常:“Illuminate\Database\QueryException” 文件:“D:\magang\serverapi\vendor\laravel\framework\src\Illuminate\Database\Connection.php” 线路:678 消息:“SQLSTATE [23000]:完整性约束违规:1048 列 'namaproduk' 不能为空(SQL:更新
produkdatas设置namaproduk= ?、deskripsi= ?、harga= ?、produkdatas。updated_at= 2021-04-09 01:50:07 其中id= 13)" trace: [{file: "D:\magang\serverapi\vendor\laravel\framework\src\Illuminate\Database\Connection.php",…},…]
【问题讨论】:
-
这个问题本质上是服务器端的。读取例外
Integrity constraint violation: 1048 Column 'namaproduk' cannot be null您的 DBMS 中有一个约束,防止列namaproduk为空。这意味着没有数据被添加到该列。向该列添加一些数据,它会通过删除表定义中的not null来插入或更改列约束以允许为空。如果那是您的 blob 列,请确保您在服务器端正确处理 blob -
我试过了,但是我要改成value的数据提交后变成了null
-
这是服务器端问题,不是 Axios 或 Vue 问题。此问题源于尝试将 blob 错误地插入到您正在使用的任何数据库中。这是 PHP 和 Mysql 的示例phppot.com/php/mysql-blob-using-php
-
但是当添加数据成功时