NodeJS从Byte Array编写JPG

时间:2016-07-27 19:49:25

标签: php node.js actionscript-3 jpeg fs

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

as3 jpeg-encoder来自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”完成了任务,但我需要将功能移到节点服务器上。

2 个答案:

答案 0 :(得分:0)

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

代码:

//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();
    }
}

答案 1 :(得分:0)

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

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