2016-06-03 2 views
0

업로드 기능에 문제가 있습니다. 내 사건이야. SQL 쿼리, 해당 쿼리를 사용하는 .xlsx 보고서를 생성하는 node.js 작업 및 생성 된 보고서를 저장하는 업로드 작업이 있습니다. 내 문제는 node.js 코드 (BackandSdk 사용)에서 업로드 작업을 호출하면 파일 내용이 실제 이진 데이터가 아닌 8 진수로 기록된다는 것입니다. 하지만 정확히 동일한 JSON 본문을 브라우저에서 호출하면 모든 것이 올바르게 작동합니다. 다음은 발췌 다운로드 한 파일에서입니다 : 내가 잘못 뭘하는지에뒤로 및 node.js 작업에서 파일을 업로드하려면 어떻게해야합니까?

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... 

어떤 아이디어?

UPDATE : Node.js를 동작 (이 질문과 관련된 코드 만)의 Z :

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

답변

0

문제 : 실제 업로드를 수행

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 함수 해결, 고마워. 기본적으로 버퍼에 사용 된 'utf8'인코딩 때문에 발생했습니다. new Buffer(reportData, 'binary')으로 지정하면 문제가 해결되었습니다.

관련 문제