2012-11-12 3 views
5

nodeJS를 사용하여 base64 문자열에 저장된 처리 된 이미지를 저장하려고합니다.NodeJS writeStream 빈 파일

var buff = new Buffer(base64data,'base64'); 
console.log(base64data); 

var stream = fs.createWriteStream('/path/to/thefile.png'); 
stream.write(buff) 
stream.end() 

그러나 결과 파일은 비어 있습니다.

console.log(base64data);의 출력을 가져 와서 로컬에서 디코딩하면 유효한 png 바이너리가 생성되므로 파일이 비어 있습니다. 왜 그렇습니까?

파일이 3600x4800 px png 파일 (즉, 거대합니다.)이 요소 일 수 있습니까?

또한 writeFile도 시도했지만 행운이 없습니다.

그리고 네, fs 너무 빨리 아무것도 기록되지 않습니다으로 require('fs')

감사

+0

코드가 유효한지, 그리고, 그것은 로컬 작업을 진행와 buff'은 당신이 쓰기를 시도 (안'base64data')을하는 것입니다'로그인 시도 할 수 있습니다 파일을 변환하여 문제가 있는지 확인하십시오. 또한 권한을 확인해보십시오. – Sdedelbrock

답변

8

당신의 stream.end()입니다. 그것은 비동기 함수 기억입니다.

var buff = new Buffer(base64data,'base64'); 
console.log(base64data); 

var stream = fs.createWriteStream('/path/to/thefile.png'); 
stream.write(buff); 
stream.on("end", function() { 
    stream.end(); 
}); 
1

더 나은 :

var buff = new Buffer(base64data,'base64'); 
console.log(base64data); 

var stream = fs.createWriteStream('/path/to/thefile.png'); 
stream.write(buff); 
stream.end(); 
stream.on('finish',() => { 
    //'All writes are now complete.' 
}); 
stream.on('error', (error) => {...}); 
+1

추가 한 행에 대한 세부 정보를 제공 할 수 있습니까? – jhhoff02

+0

Node.js의 설명서를 읽어보십시오. –