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.
[10:29:56] INFO (payload): Starting Payload...
[10:29:57] ERROR (payload): There were 1 errors validating your Payload config
[10:29:57] ERROR (payload): 1: Collection "media" > "upload.defParamCharset" is not allowed
Why apear these error messages, after I change the config?
Hi @RiceeeChang - you don't need to add defParamCharset
to your uploads collection config, instead it needs to be added to your payload.config.ts. If you remove it from your media collection, and add it into the payload.config, the error should go away.
thanks,
The problem is solved, and the fileupload with chinese filename is working well.
Hey @clhome, I'm looking into this now 🔎
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.
[10:29:56] INFO (payload): Starting Payload...
[10:29:57] ERROR (payload): There were 1 errors validating your Payload config
[10:29:57] ERROR (payload): 1: Collection "media" > "upload.defParamCharset" is not allowed
Why apear these error messages, after I change the config?
Hi @RiceeeChang - you don't need to add defParamCharset
to your uploads collection config, instead it needs to be added to your payload.config.ts. If you remove it from your media collection, and add it into the payload.config, the error should go away.
thanks,
The problem is solved, and the fileupload with chinese filename is working well.
Star
Discord
online
Get help straight from the Payload team with an Enterprise License.