上传图片之后,图片路径没办法存进数据库表中,请问是什么原因呢
<el-col :span="8">
<el-form-item label="物资图片" prop="unit">
<el-upload
accept="image/jpeg,image/png"
:on-preview="handlePreview"
:on-remove="handleRemove"
:before-remove="beforeRemove"
ref="upload"
:action="'http://localhost:8989/api/uploadImage'"
:http-request="upload"
:auto-upload="false"
:before-upload="onBeforeUpload"
multiple
:limit="1"
:on-exceed="handleExceed"
v-model="addForm.pictures">
<el-button size="small" type="primary">点击上传el-button>
<div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过1Mdiv>
el-upload>
el-form-item>
el-col>
<el-form-item>
<el-button type="primary" @click="addIn">立即捐赠el-button>
el-form-item>
methods:{
addIn() {
this.$refs.upload.submit();
this.$refs.addFormRef.validate(async valid => {
if (!valid) return;
const {data: res} = await this.$http.post("addIn2", this.addForm);
if (res != "success") {
return this.$message.error("捐赠失败~");
}
this.$message.success("捐赠成功~");
console.log(this.addForm);
this.addDialogVisible = false;
});
},
handleRemove(file, fileList) {
console.log(file, fileList);
},
handlePreview(file) {
console.log(file);
},
handleExceed(files, fileList) {
this.$message.warning(`当前限制选择 1 个文件,本次选择了 ${files.length} 个文件,共选择了 ${files.length + fileList.length} 个文件`);
},
beforeRemove(file, fileList) {
return this.$confirm(`确定移除 ${ file.name }?`);
},
onBeforeUpload(file)
{
const isIMAGE = file.type === 'image/jpeg'||'image/png';
const isLt1M = file.size / 1024 / 1024 < 1;
if (!isIMAGE) {
this.$message.error('上传文件只能是图片格式!');
}
if (!isLt1M) {
this.$message.error('上传文件大小不能超过 1MB!');
}
return isIMAGE && isLt1M;
},
upload (file) {
const _this = this
let formdata = new FormData()
// 上传图片并转成Base64编码
formdata.append('files', file.file)
console.log(formdata)
_this.$http.post('/api/uploadImage', formdata).then((resp) => {
if (resp.status === 200) {
console.log(resp.data)
this.addForm.pictures = resp.data;
// 设置图片回显
_this.form.logo = resp.data
_this.$message({type: 'success', message: '图片上传成功!'})
}
}).catch(() => {
this.$message({type: 'info', message: '图片太大或格式有误,上传失败,请重新上传!'})
})
}
}
}
@RequestMapping(value = "/api/uploadImage", method = RequestMethod.POST)
@ResponseBody
public String uploadImage(@RequestParam("files") MultipartFile file) throws IOException {
System.out.println(file.getOriginalFilename() + "图片已传入!!");
byte[] b = file.getBytes();
String fileName = file.getOriginalFilename();
Path path = Paths.get("src/main/resources/pictures/" + fileName);
Files.write(path, b);
String receivePath = path.toString();
return receivePath;
}
上传图片后,图片路径无法存储到数据库表中可能有以下几个原因:
数据库表中的字段类型不匹配:如果存储图片路径的字段类型为整数类型,那么无法存储字符串类型的图片路径。需要将该字段的类型改为字符串类型,如 VARCHAR。
数据库连接异常:如果数据库连接异常,无法将数据写入数据库表中。可以检查数据库连接是否正常,以及数据库的相关配置是否正确。
SQL 语句错误:如果 SQL 语句中的表名、字段名或参数名错误,也会导致无法将数据写入数据库表中。可以检查 SQL 语句是否正确,以及参数的值是否正确。
数据库表中的字段长度不足:如果存储图片路径的字段长度不足,无法存储完整的图片路径。需要将该字段的长度扩大,以存储完整的图片路径。
代码逻辑错误:可能是代码中逻辑错误导致无法将图片路径存储到数据库表中。可以检查代码逻辑是否正确,并进行调试。
如有帮助请点击采纳
可能是以下几个原因导致图片路径无法存储到数据库表中:
解决方案:在插入数据时,将图片路径的值赋给对应的栏位,例如:
INSERT INTO table_name (column1, column2, image_path) VALUES (value1, value2, 'path/to/image.jpg');
解决方案:扩大图片路径栏的长度,或者使用BLOB类型的栏位存储图片。
解决方案:确保插入的图片路径格式正确,例如使用绝对路径或相对路径,并且在插入数据时需要将路径用引号括起来。
例如:
INSERT INTO table_name (column1, column2, image_path) VALUES (value1, value2, '/path/to/image.jpg');
解决方案:检查数据库表的权限设置,确保具有插入数据的权限。例如:
GRANT INSERT ON database_name.table_name TO 'user'@'localhost';
以上是可能导致图片路径无法存储到数据库表中的一些原因及其解决方案,希望能够帮助到提问者解决问题。