[英]PayloadTooLargeError: request entity too large when upload image
I am trying to upload/and save an image in base64 format to my mongo database.我正在尝试将 base64 格式的图像上传/并保存到我的 mongo 数据库中。 If I use a very very small image it works, but I try to use an image of 161 kb, I have this error:
如果我使用非常非常小的图像,它可以工作,但我尝试使用 161 kb 的图像,则会出现以下错误:
PayloadTooLargeError: request entity too large
PayloadTooLargeError:请求实体太大
So I try to convert my image with Json but I got an error or it doesn't work, Her my code ( I am using vue):所以我尝试用 Json 转换我的图像,但出现错误或不起作用,她的代码(我正在使用 vue):
<template>
<div class="profile">
<div class="px-4">
<div class="row justify-content-center">
<div class="col-lg-3 order-lg-2">
<div class="card-profile-image image-preview">
<div v-if="profImage !=undefined && profImage.length > 0">
<a>
<img
:src="profImage"
class="rounded-circle"
/>
</a>
</div>
<div>
<div class="file-upload-form">
Upload image:
<input
type="file"
@change="previewImage"
accept="image/*"
/>
</div>
<div class="image-preview" v-if="imageData.length > 0">
<img class="preview" :src="imageData" />
<button @click="updateUserImage"></button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</template>
Here my js file:这是我的 js 文件:
<script>
import DataService from '@/services/DataService'
export default {
name: 'Profile',
data: function() {
return {
username: '',
imageData: '',
profImage: '',
}
},
methods: {
previewImage: function(event) {
var input = event.target
if (input.files && input.files[0]) {
var reader = new FileReader()
reader.onload = e => {
this.imageData = e.target.result
}
reader.readAsDataURL(input.files[0])
}
},
async getAllInfo() {
var userImage = await DataService.getUserImage({
username: this.username
})
this.profImage = userInfo.data.user[0].profImage //IT Works
this.profImage = JSON.parse(userInfo.data.user[0].profImage) //I get an error
},
async updateUserImage() {
var temp = JSON.stringify(this.imageData)
console.log(this.firstname)
await DataService.updateUserInfo({
username: this.username,
user: {
profImage: temp
}
})
}
},
mounted() {}
}
</script>
When I try to use "JSON.parse(userInfo.data.user[0].profImage)"I get an error :当我尝试使用“JSON.parse(userInfo.data.user[0].profImage)”时,出现错误:
"Unexpected token d in JSON at position 0"
“JSON 中位置 0 的意外标记 d”
I also try with JSON.Stringify, but I get is not a function.我也尝试使用 JSON.Stringify,但我得到的不是函数。
In my db, the image is saved in this way:在我的数据库中,图像以这种方式保存:
profImage: {
image: Buffer,
require: false
},
What am I doing wrong?我究竟做错了什么? I am using mongodb, vue , express and node.
我正在使用 mongodb、vue、express 和 node。
我使用bodyParser.json({ limit: "50mb" })
更改解析器限制并为我工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.