μ΄ μμ μ μνν μ μλ npmμ λΌμ΄λΈλ¬λ¦¬κ° μμ΅λκΉ?
axios.get('http://www.freeiconspng.com/uploads/profile-icon-9.png', {
transformRequest: [function (data) {
return data;
}],
transformResponse: [function (data) {
return data;
}],
});
μ΄κ²μ StackOverflowμ λν μ§λ¬Έμ΄λΌκ³ μκ°ν©λλ€.
μ루μ μ κ²μνλ μ¬λμ μν΄ λ€μ μ½λκ° μμ΅λλ€.
get('/getImg', {
responseType: 'arraybuffer'
})
.then(function(result) {
console.log(_imageEncode(result.data))
})
function _imageEncode (arrayBuffer) {
let u8 = new Uint8Array(arrayBuffer)
let b64encoded = btoa([].reduce.call(new Uint8Array(arrayBuffer),function(p,c){return p+String.fromCharCode(c)},''))
let mimetype="image/jpeg"
return "data:"+mimetype+";base64,"+b64encoded
}
μ΄κ²μ μ°λ¦¬μ μ루μ μ΄μκ³ λ§€λ ₯μ²λΌ μλν©λλ€.
return axios.get('http://example.com/image.png', { responseType: 'arraybuffer' })
.then((response) => {
let image = btoa(
new Uint8Array(response.data)
.reduce((data, byte) => data + String.fromCharCode(byte), '')
);
return `data:${response.headers['content-type'].toLowerCase()};base64,${image}`;
});
μ΄κ²μ΄ μλνμ§ μλ μ΄μ λ axiosκ° λͺ¨λ κ²μ json κ°μ²΄μ μ μ₯νκΈ° λλ¬Έμ λλ€. λ°μ΄λ리λ₯Ό json κ°μ²΄μ λ£μ μ μκΈ° λλ¬Έμ λ°μ΄λ리λ₯Ό κΉ¨λ λ¬Έμμ΄λ‘ λ³νν©λλ€.
μ΄κ²μ λλ₯Ό μν΄ μλ²½νκ² μλνμ΅λλ€.
function getBase64(url) {
return axios
.get(url, {
responseType: 'arraybuffer'
})
.then(response => new Buffer(response.data, 'binary').toString('base64'))
}
λμ κ°μ λ Έλμ μκ³ v >= 6 μ μ¬μ©νλ κ²½μ° λ³κ²½ μ¬νμ΄ μμΌλ―λ‘ Buffer.from λλ μ΄μ μ μ¬ν κ²μ μ¬μ©ν΄μΌ ν©λλ€.
function getBase64(url) {
return axios
.get(url, {
responseType: 'arraybuffer'
})
.then(response => Buffer.from(response.data, 'binary').toString('base64'))
}
μ°Έμ‘° μμ λ²νΌ
new Buffer()' was deprecated since v6. Use 'Buffer.alloc()' or 'Buffer.from()' (use 'https://www.npmjs.com/package/safe-buffer' for '<4.5.0') instead
@sean-hill μ΄λ―Έμ§μ 첫 λ²μ§Έ ν ν°μ μ»μ μ μλ μ΄μ κ° λ¬΄μμΈμ§ μμλκΉ? μ΄λ€ μ΄μ λ‘ data:image/png;base64,
λΆλΆμ΄ νμλμ§ μμ base64 λ°μ΄ν°κ° μ ν¨νμ§ μμ΅λλ€.
λλ₯Ό μν μ΄ μν
function getImage(imageUrl) {
var options = {
url: `${imageUrl}`,
encoding: "binary"
};
return new Promise(function (resolve, reject) {
request.get(options, function (err, resp, body) {
if (err) {
reject(err);
} else {
var prefix = "data:" + resp.headers["content-type"] + ";base64,";
var img = new Buffer(body.toString(), "binary").toString("base64");
// var img = new Buffer.from(body.toString(), "binary").toString("base64");
var dataUri = prefix + img;
resolve(dataUri);
}
})
})
}
function getImage(imageUrl) {
var options = {
url: `${imageUrl}`,
encoding: "binary"
};
request.get(options, function (err, resp, body) {
if (err) {
reject(err);
} else {
var prefix = "data:" + resp.headers["content-type"] + ";base64,";
var img = new Buffer(body.toString(), "binary").toString("base64");
// var img = new Buffer.from(body.toString(), "binary").toString("base64");
var dataUri = prefix + img;
}
})
}
return axios.get('http://site.com/img.png', { responseType: 'arraybuffer' })
.then(response => `data:${response.headers['content-type']};base64,${btoa(String.fromCharCode(...new Uint8Array(response.data)))}`);
return axios.get('http://site.com/img.png', { responseType: 'arraybuffer' }) .then(response => `data:${response.headers['content-type']};base64,${btoa(String.fromCharCode(...new Uint8Array(response.data)))}`);
λ Έλ λ²μ :
return axios.get(url, { responseType: 'arraybuffer' }).then(res => {
;`data:${res.headers['content-type']};base64,${Buffer.from(String.fromCharCode(...new Uint8Array(res.data)), 'binary')
.toString('base64')}`
})
@voidpls
κ·νμ λ°©λ²μ μ€λ₯λ₯Ό μ λ°ν©λλ€!
Uncaught RangeError: Maximum call stack size exceeded
κ·Έκ²μ μμΈ
toa(String.fromCharCode(...new Uint8Array(response.data)))
μ΄ μ¬μ§μ μλ ν μ μμ΅λλ€
κ°μ₯ μ μ©ν λκΈ
λμ κ°μ λ Έλμ μκ³ v >= 6 μ μ¬μ©νλ κ²½μ° λ³κ²½ μ¬νμ΄ μμΌλ―λ‘ Buffer.from λλ μ΄μ μ μ¬ν κ²μ μ¬μ©ν΄μΌ ν©λλ€.
μ°Έμ‘° μμ λ²νΌ
new Buffer()' was deprecated since v6. Use 'Buffer.alloc()' or 'Buffer.from()' (use 'https://www.npmjs.com/package/safe-buffer' for '<4.5.0') instead