I have two pieces of code:
我有两段代码:
var mmmagic = require('mmmagic');
var request = require('request');
var magic = new mmmagic.Magic(mmmagic.MAGIC_MIME_TYPE);
data = fs.readFileSync('/Users/myaccount/Desktop/test.png');
magic.detect(data,function(err,mime){
console.log(mime); // prints 'image/png'
}
and
和
var mmmagic = require('mmmagic');
var request = require('request');
var magic = new mmmagic.Magic(mmmagic.MAGIC_MIME_TYPE);
request.get('https://www.google.pl/images/srpr/logo11w.png',function(err,res,data){
data = new Buffer(data); // tried also new Buffer(data,'binary');
magic.detect(data,function(err,mime){
console.log(mime); // prints 'application/octet-stream'
}
})
So first one checks mime type of file from local disk and its 'image/png'. The second one downloads image from url (its google logo in png format) from url and check its mime type and it is 'application/octet-stream'.
首先,从本地磁盘及其“image/png”检查mime类型的文件。第二个从url(它的谷歌标识为png格式)下载图像,检查它的mime类型,它是“application/octet-stream”。
How I can convert response from request
(which is a string) to a binary buffer so mime detection would return 'image/png'??
如何将请求(即字符串)的响应转换为二进制缓冲区,以便mime检测返回'image/png'?
1 个解决方案
#1
3
You have to pass in the option encoding: null
您必须传递选项编码:null。
var mmmagic = require('mmmagic')
, request = require('request')
, magic = new mmmagic.Magic(mmmagic.MAGIC_MIME_TYPE)
, image = 'https://www.google.pl/images/srpr/logo11w.png';
request({
uri: image,
encoding: null
}, function(err, res, data) {
console.log(typeof data);
console.log(data.constructor);
magic.detect(data, function(err,mime) {
console.log(mime); // prints 'image/png'
});
});
I noticed that data
was a string when using request.get(<urlString>)
. For debugging purposes I used typeof <something>
& <something>.constructor
to determine what <something>
really was.
我注意到使用request.get时数据是一个字符串(
The docs are a little misleading stating that
这些文档有点误导人
encoding - Encoding to be used on setEncoding of response data. If null, the body is returned as a Buffer.
编码——用于对响应数据进行setEncoding的编码。如果为空,则返回主体作为缓冲区。
Making one think that the default would be a Buffer!
让人认为默认是一个缓冲!
#1
3
You have to pass in the option encoding: null
您必须传递选项编码:null。
var mmmagic = require('mmmagic')
, request = require('request')
, magic = new mmmagic.Magic(mmmagic.MAGIC_MIME_TYPE)
, image = 'https://www.google.pl/images/srpr/logo11w.png';
request({
uri: image,
encoding: null
}, function(err, res, data) {
console.log(typeof data);
console.log(data.constructor);
magic.detect(data, function(err,mime) {
console.log(mime); // prints 'image/png'
});
});
I noticed that data
was a string when using request.get(<urlString>)
. For debugging purposes I used typeof <something>
& <something>.constructor
to determine what <something>
really was.
我注意到使用request.get时数据是一个字符串(
The docs are a little misleading stating that
这些文档有点误导人
encoding - Encoding to be used on setEncoding of response data. If null, the body is returned as a Buffer.
编码——用于对响应数据进行setEncoding的编码。如果为空,则返回主体作为缓冲区。
Making one think that the default would be a Buffer!
让人认为默认是一个缓冲!