读取多部分/表单数据正在不连贯地保存



我正在尝试从http图像文件上传中读取数据。它似乎读取了文件,但由于某种原因,输出文件的大小大约是原始文件的两倍。

原始文件: 94.7KB
临时文件: 168.8 KB

当我在文本编辑器中打开这两个文件时,它们的行数相同,并且开头和结尾字符相同。这让我相信上传的文件被保存为字符串而不是二进制数据。

我认为重要的部分如下

数据的读取:

let body = ''
req.on('data', data => {
body += data.toString()
}).on('end', data => {
if (data) body += data.toString()
// Rest of block
})

数据的保存:

// If there is a filename grab the file data
if (result.filename.length > 0) {
// Create a temporary url
let temp = join(os.tmpdir(), (Math.random() * 10000).toString(12).substr(5, 10))
// Get the data between the blocks after the first two newlines
let matches = item.match(/^.+?(rnrn|nn)(.+)/s)
// Write the data to file
fs.createWriteStream(temp).write(matches[2])
}

以下是数据的完整解析:

http.createServer((req, res) => {
let body = ''
req.on('data', data => {
body += data.toString()
}).on('end', data => {
if (data) body += data.toString()
let boundary = req.headers['content-type'].split('boundary=')[1]
// Split all the boundary items and loop over them
body.split(new RegExp(`(--${boundary}|--${boundary}--)`)).forEach(item => {
if (item.trim().toLowerCase().startsWith('content-disposition')) {
item = item.trim()
// Find the name and filename
let result = item.split(':')[1].split(';').map(i => i.trim()).reduce((obj, itm) => {
if (itm.startsWith('name=')) obj.name = itm.match(/^name="(.+)"/)[1]
if (itm.startsWith('filename=')) obj.filename = itm.match(/^filename="(.+)"/)[1]
return obj
}, { name: '', filename: '' })
// If there is a filename grab the file data
if (result.filename.length > 0) {
// Create a temporary url
let temp = join(os.tmpdir(), (Math.random() * 10000).toString(12).substr(5, 10))
// Get the data
let matches = item.match(/^.+?(rnrn|nn)(.+)/s)
// Write the data to file
fs.createWriteStream(temp).write(matches[2])
}
}
})
})
})

所以,我在这两种情况下都是正确的......

首先,我必须将数据读取为二进制数据,如下所示:

body += data.toString('binary')

接下来保存时,我需要像这样保存为二进制数据:

fs.createWriteStream(temp).write(matches[2], 'binary')

现在,这将保存为正确的文件大小,并且图像是可读的!

最新更新