When I upload an image and the file name is in Chinese, garbled characters appear on the webpage. How can I solve this problem?
this is the image link
I searched online and found that if the file name appears garbled when uploading using Node FormData, it may be because the file name is not encoded in UTF-8. We need to use the encodeURI() function or other encoding methods to solve this problem.like this:
const form = new FormData();
form.append('file', fs.createReadStream(filePath), encodeURI(fileName));
However, I don’t know where to place these codes. Does anyone know?
To fix this issue, add the following property to the upload
object in your payload.config
:
upload: {
defParamCharset: 'utf-8',
},
Here is a little background as to why...
Payload uses the express-fileupload package to manage uploads. By default, this package has defParamCharset
set to latin1
. However, we need a character set that supports Chinese i.e. utf-8
.
You have full control over the express-fileupload
options with Payload, simply pass any valid properties to the upload object in your base payload.config
.
Note: here is the defParamCharset
definition from express-fileupload
:
Thank you very much, the problem is solved, this problem has been bothering me for a long time.
Hey @clhome, I'm looking into this now
Star
Discord
online
Get help straight from the Payload team with an Enterprise License.