Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Node.js读取和写入二进制数据时出现问题_Node.js_File Io - Fatal编程技术网

使用Node.js读取和写入二进制数据时出现问题

使用Node.js读取和写入二进制数据时出现问题,node.js,file-io,Node.js,File Io,寻找一些新的NodeJS帮助。一定有一些简单的事情我忽略了,但我试着用几种方法去做,结果总是很奇怪 我从中获取了以下大部分代码 // 128Kb Chunks var targetSize = 131072; // Open the file fs.open('/project/input.png', 'r', function(err, fd) { // Gather some info about the file fs.fstat(fd, function(err, s

寻找一些新的NodeJS帮助。一定有一些简单的事情我忽略了,但我试着用几种方法去做,结果总是很奇怪

我从中获取了以下大部分代码

// 128Kb Chunks
var targetSize = 131072;

// Open the file
fs.open('/project/input.png', 'r', function(err, fd) {

    // Gather some info about the file
    fs.fstat(fd, function(err, stats) {
        var bufferSize=stats.size,
            chunkSize=targetSize,
            buffer=new Buffer(bufferSize),
            bytesRead = 0,
            isFinal = false;

        while (bytesRead < bufferSize) {

            if ((bytesRead + chunkSize) > bufferSize) {
                chunkSize = (bufferSize - bytesRead);
                isFinal = true;
            } else {
                isFinal = false;
            }

            fs.read(fd, buffer, bytesRead, chunkSize, bytesRead);

            bytesRead += chunkSize;

            if( isFinal ) {
                fs.writeFileSync("/project/output.png", buffer);
            }

        }

    });

    // Done
    fs.close(fd);

});
试着换线

while (bytesRead < bufferSize)

因为isFinal无论如何都是错误的(在这一行中你永远不会改变它)。

所有好的建议,我非常感谢,但正如你所警告的,它们对结果没有影响:\
while (bytesRead < bufferSize)
while (bytesRead < bufferSize && !isFinal)
} else {
    isFinal = false;
}