在 Back 以及如何通过 node.js 操作上传文件?

In Backand how do I upload a file from node.js action?

我在使用上传功能时遇到问题。这是我的情况。我有:一个 SQL 查询,一个使用该查询生成 .xlsx 报告的 node.js 操作,以及一个用于存储生成的报告的上传操作。我的问题是,如果我从 node.js 代码调用上传操作(使用 BackandSdk),文件内容将以八位字节而不是实际的二进制数据写入。但是当我使用完全相同的 JSON 主体从浏览器调用它时,一切正常。 以下是下载文件的摘录:

504b 0304 0a00 0000 0000 0000 2100 3b48
8e40 c404 0000 c404 0000 1300 0000 5b43
6f6e 7465 6e74 5f54 7970 6573 5d2e 786d
6c3c 3f78 6d6c 2076 6572 7369 6f6e 3...

对我做错了什么有什么想法吗?

更新: node.js动作代码(仅与本题相关的代码):

var BackandSdk = require('./backand');
var backand = new BackandSdk();

var masterToken = "<master token>"; //<put here the master token that you run in the action init>;
var userToken = "<user token>"; //<put here the user token that you run in the action init>;
var token = masterToken + ":" + userToken;

exports.generateMonthlyReport = generateMonthlyReport;

function generateMonthlyReport(month, userId) {    
    return backand.basicAuth(token)
        .then(function () {
            return backand.get('/1/query/data/monthlyReport', {
                month: month
            });
        })
        .then(function (result) {
            // build xlsx report using xlsx-template
            // returns promise that resolves with binary data
            return generateXls('monthly-timelog.xlsx', {
                data: result,
                total: sumTotal(result)
            });
        })
        .then(function (reportData) {
            var name = 'timelog-' + month.toLowerCase() + '.xlsx';
            return deleteReport(name).then(function () {
                return uploadReport(name, reportData);
            });
        })
        .catch(function (err) {
            console.log(err.stack);
            return q.reject(err);
        });

    function sumTotal(rows) {
        return rows.reduce(function (total, row) {
            return total + row.total;
        }, 0);
    }
}

执行实际上传的 uploadReport 函数:

function uploadReport(reportName, reportData) {
    var base64 = new Buffer(reportData).toString('base64');
    return backand.api.post('/1/objects/action/timeEntries?name=uploadReport', {
        filename: reportName,
        filedata: base64
    }).then(function (res) {
        console.log(res);
        return res;
    });
}

问题已解决,谢谢。这是由 Buffer 默认使用 'utf8' 编码引起的。将其指定为 new Buffer(reportData, 'binary') 已解决问题。