2017-02-10 2 views
0

다운로드 한 이미지가 왜 잘못 되었습니까?다운로드 한 이미지가 왜 잘못 되었습니까?

import * as fs from 'fs'; 
import * as request from 'request-promise-native'; 

const download = async (url) => { 
    console.log(`Downloading ${url}`); 
    const options = { 
    url, 
    resolveWithFullResponse: true, 
    }; 
    const response = await request.get(options); 
    console.dir(response.headers); 
    return fs.writeFileSync('image.jpg', response.body); 
}; 


const main = async() => { 
    try { 
    await download('https://dz2cdn1.dzone.com/storage/rc-covers/3339976-refcard-cover141.png'); 
    } catch (e) { 
    console.error(e); 
    } 
}; 

main().then(() => console.log('success')).catch((e) => console.log(e)); 

결과 이미지의 형식이 잘못되어 열 수 없습니다. 문제를 일으키는 원인과 해결 방법에 대한 아이디어가 있습니까?

+0

[요청을 사용하여 Node.js의 바이너리 컨텐츠 가져 오기] (http://stackoverflow.com/questions/14855015/getting-binary-content-in-node-js-using-request) – GilZ

답변

1

기본적으로 request은 응답을 utf-8 텍스트로 처리합니다. 응답을 이진 형식 (특히 Buffer)으로 유지하려면 request() 옵션에 encoding: null을 명시 적으로 설정해야합니다.

+0

감사합니다! 그것은 그것을 고쳤다! – Kiril

관련 문제