Вопрос

I am using the node-png library to make the png and then save it in the local directory but when I go to open it back up, it says that it does not exist. I want to either read in the data and send it out or just have the response send an <img> field with the picture. Here's what I have so far:

//write out img data
png.pack().pipe(dst);

//link to new image made
var link = fs.createReadStream('out.png'); //says that this does not exist

//write out response with img then delete file
response.writeHead(200, {"Content-Type": "image/png"});
response.end(link, 'binary');
Это было полезно?

Решение

Couldnt you just skip writing to file altogether?

var output = png.pack();
res.setHeader('Content-Type', 'image/png');
output.pipe(res);

If you do need to write it to disk simultaneously, refer to this answer:

https://stackoverflow.com/a/19561718/944006

If you need to write to disk FIRST and THEN read from that, you would do:

var output = png.pack();
output.pipe(dst, { end: false });
output.on('end', function(){
     //Read your file and set your response here.
}

I didn't test any of this but that's the way piping works in general.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top