NodeJS 从字节数组写入 JPG

NodeJS Writing JPG from Byte Array

我有一个 AS3 应用程序从 http post 向我发送一个 jpeg 编码的字节数组。我的节点应用程序将采用它,并将其作为 jpg 格式保存在本地文件系统中。

as3 jpeg 编码器来自 as3 core lib

代码:

var results = new Object();
    results.success = false;
    results.message = debugWrite("Post Recieved \r\n");
    processPost(request, response, function() {

        results.message += debugWrite("path: " + FILEPATH + "\r\n");

        //save jpg
        var buffer = new Buffer(request.post, 'binary');
        fs.writeFile(FILEPATH, buffer, "binary", function(error){
            if(error != null){
                results.message +=debugWrite("Error: "+ error + "\r\n");
            }else{
                results.message += debugWrite("OK - Saved");
                results.success = true;
            }
        });

        response.writeHead(200, "OK", {'Content-Type': 'application/json'});
        response.write(JSON.stringify(results));
        response.end();
    });
}

var processPost = function(request, response, callback) {
    var queryData = "";
    if(typeof callback !== 'function') return null;

    if(request.method == 'POST') {
        request.on('data', function(data) {
            queryData += data;
            if(queryData.length > 1e6) {
                debugWrite("Request To Large");
                queryData = "";
                response.writeHead(413, {'Content-Type': 'text/plain'}).end();
                request.connection.destroy();
            }
        });

        request.on('end', function() {
            request.post = queryData;
            callback();
        });

        } else {
            response.writeHead(405, {'Content-Type': 'text/plain'});
            response.end();
        }
}

当前正在保存文件,但是该文件不可查看。 非常感谢任何提示。

php 使用 "file_put_contents" 完成了任务,但我需要将功能移动到节点服务器。

来自数据事件请求的数据是完美的,所以我使用了 writestream。不知何故,收集所有数据弄乱了信息。

代码:

//server handling route
if(QD === "/postImage"){
    processPost(request, response, filename);
}

var processPost = function(request, response, name) {
    if(request.method == 'POST') {
        //repsonse object
        var results = new Object();
        results.success = false;
        results.message = debugWrite("Post Recieved \r\n");
        results.message += debugWrite("HREF: " + name + "\r\n");

        //make write stream
        var ImageFile = fs.createWriteStream(PATH+'/public/assets/'+name);

        //on data
        request.on('data', function(data) {
            //write to file
            ImageFile.write(data);
        });

        //on end
        request.on('end', function() {
            //finish write
            ImageFile.end();
            response.writeHead(200, "OK", {'Content-Type': 'application/json'});
            response.write(JSON.stringify(results));
            response.end();
        });

    } else {
        response.writeHead(405, {'Content-Type': 'text/plain'});
        response.end();
    }
}

在请求中添加 encoding: 'binary' 解决了 (fd fd fd fd....)

的问题
request.get({url: url_snapshot, encoding: 'binary'}