简体   繁体   English

NodeJS缓冲区Base64文件限制

[英]NodeJS Buffer Base64 File Limits

Tried to upload an image using Base64 but if the string is long the bottom of the stored image is blank white. 尝试使用Base64上传图像,但是如果字符串较长,则存储图像的底部为空白白色。

upload.js upload.js

var uuid = require('node-uuid');
var fs = require('fs');

exports.uploadImage = function(base64Str, callback) {
    var filename = uuid.v1()+'.png';
    var bitmap = new Buffer(base64Str, 'base64');
    fs.writeFileSync(filename, bitmap);
    callback(filename);
};

server.js server.js

var fs = require('fs');
var restify = require('restify');  
var server = restify.createServer();
server.use(restify.bodyParser());

var upload = require('./modules/upload');

server.post('/images', function(req, res) {
    upload.uploadImage(req.params.myImage, function(filename) {
        console.log('processing image');
        res.send(filename);
        res.end();
    });
});

server.listen(3000);

It works for a string of length 499016 bytes but not if the string is 847508 bytes. 它适用于长度为499016字节的字符串,但不适用于该字符串为847508字节的字符串。 Is there a documented size limit and, if not, how can I upload and un-encode longer strings? 是否有文件记载的大小限制?如果没有,我如何上传和取消编码较长的字符串?

You might be hitting a request size limit. 您可能正在达到请求大小限制。 One alternative would be to upload your image as multipart/form-data . 一种选择是将您的图像作为multipart/form-data You can do this with the mapFiles options in restify.bodyParser , and you can test it by choosing file instead of text in your Postman form-data. 您可以使用mapFiles中的mapFiles选项执行此restify.bodyParser ,并且可以通过选择文件而不是Postman表单数据中的文本来进行测试。

Edited: fs.rename might make more sense here for testing purposes. 编辑: fs.rename在这里出于测试目的可能更有意义。 In a practical application you might want to save uploaded files elsewhere, on s3 perhaps. 在实际的应用程序中,您可能想将上传的文件保存在其他位置,也许在s3上。

var fs = require('fs');
var restify = require('restify');
var server = restify.createServer();
server.use(restify.bodyParser({
    mapFiles: true
}));

var uuid = require('node-uuid');
var fs = require('fs');

var uploadImage = function(path, callback) {
    var filename = uuid.v1() + '.png';
    fs.rename(path, filename, function(err) {
        if (err) {
            throw err;
        }
        callback(filename);
    });
};

server.post('/images', function(req, res) {
    uploadImage(req.files.myImage.path, function(filename) {
        res.send(filename);
        res.end();
    });
});

server.listen(3000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM