0
閱讀圖像文件時應該使用什麼編碼?我構建了一個基本的服務器來提供一個帶有圖像的html文件,當我用'utf-8'編碼讀取圖像文件並將數據發送到瀏覽器時,瀏覽器不會收到圖像。但是,如果我將編碼設置爲空字符串(即{編碼:''}),瀏覽器將接收圖像。另外,當我查看讀取流對象時,它說缺省編碼是'utf-8',這讓我想知道爲什麼將編碼設置爲'utf-8'不起作用?如何知道讀取文件時使用什麼編碼?
下面是一段代碼,不工作:
let readStream = fs.createReadStream('./static/002.jpg', {flags: 'r', encoding: 'utf8'})
console.log('ReadStream: ', readStream)
res.setHeader('Content-Type', 'image/jpeg')
readStream.on('data', (chunk) => {
res.write(chunk)
})
readStream.on('error', (err) => {
console.log(err)
})
readStream.on('end',() => {
console.log('Image stream ended')
res.end()
})
readStream.on('close', function(){
console.log("Image Stream closed")
})
而這裏的一段代碼,做工作:
let readStream = fs.createReadStream('./static/002.jpg', {flags: 'r', encoding: ''})
console.log('ReadStream: ', readStream)
res.setHeader('Content-Type', 'image/jpeg')
readStream.on('data', (chunk) => {
res.write(chunk)
})
readStream.on('error', (err) => {
console.log(err)
})
readStream.on('end',() => {
console.log('Image stream ended')
res.end()
})
readStream.on('close', function(){
console.log("Image Stream closed")
})