上传压缩包文件名为【A你你】,node后端获取到文件名变成了【A浣犱綘】。欢迎大神留言指导。
前端文件上传的post请求信息如下:
服务器端代码如下:
采用iconv尝试了多种编码和解码方法,还是没能正确还原字符串。对照了utf-8汉字编码是正确的,解码结果不正确。
var buf = new Buffer( file.name.split('.')[0], 'UTF-8');
// console.log(buf.toString('UTF-8'));
console.log(buf);
buf = iconv.decode(buf,'UTF-8');
console.log(buf);
// var temp = iconv.decode(buf,'utf-8');
console.log("gbkBytes=======================");
var gbkBytes = iconv.encode(file.name.split('.')[0],'gbk');
console.log(gbkBytes);
gbkBytes = iconv.decode(gbkBytes,'gbk');
console.log(gbkBytes);
console.log("binary=======================");
var binary = iconv.encode(file.name.split('.')[0],'binary');
console.log(binary);
binary = iconv.decode(binary,'UTF-8');
console.log(binary);
console.log("end=======================");
后台输出如下
utf-8编码=======================
<Buffer 41 e4 bd a0 e4 bd a0>
QeS9oOS9oA==
gbk编码=======================
<Buffer 41 c4 e3 c4 e3>
A浣犱綘
binary编码=======================
<Buffer 41 60 60>
A``
end=======================
具体文件信息如下:
File {
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
size: 160462,
path: '/tmp/upload_c4ec9eb09f55db9e6d2efce19760e034',
name: 'A浣犱綘.rar',
type: 'application/octet-stream',
hash: null,
lastModifiedDate: 2019-01-04T02:09:54.482Z,
_writeStream:
WriteStream {
_writableState:
WritableState {
objectMode: false,
highWaterMark: 16384,
finalCalled: false,
needDrain: false,
ending: true,
ended: true,
finished: true,
destroyed: false,
decodeStrings: true,
defaultEncoding: 'utf8',
length: 0,
writing: false,
corked: 0,
sync: false,
bufferProcessing: false,
onwrite: [Function: bound onwrite],
writecb: null,
writelen: 0,
bufferedRequest: null,
lastBufferedRequest: null,
pendingcb: 0,
prefinished: true,
errorEmitted: false,
bufferedRequestCount: 0,
corkedRequestsFree: [Object] },
writable: false,
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
path: '/tmp/upload_c4ec9eb09f55db9e6d2efce19760e034',
fd: null,
flags: 'w',
mode: 438,
start: undefined,
autoClose: true,
pos: undefined,
bytesWritten: 160462,
closed: true } }