2014-09-22 66 views
2

我試圖在Cloud Code中加載PDF並將其作爲附件與Mandrill一起發送。從遠程URL加載pdf以作爲Mandrill附件發送

由於mandrill需要base64編碼的字符串作爲附件,我使用Buffer來更改編碼。

現在的結果是無法打開的PDF附件,我認爲錯誤是由我的重新編碼引起的。

我猜想從get請求返回的文本是utf8,但我不確定。

有什麼建議嗎?

var Buffer = require('buffer')。Buffer;

function loadPdf(pdfUrl) { 
    var promise = new Parse.Promise(); 

    Parse.Cloud.httpRequest({ 
    method: 'GET', 
    url: pdfUrl, 
    success: function (httpResponse) { 
     // Put text into buffer 
     var buf = new Buffer('httpResponse.text', 'utf8'); 
     // encode text with base64 
     promise.resolve(buf.toString('base64')); 
    }, 
    error: function (httpResponse) { 
     console.error(httpResponse); 
     console.error("Token Request failed with response code " + httpResponse.status); 
    } 
    }); 
    return promise; 
} 

這個函數的結果被放入

"attachments": [ 
    { 
     "type": "application/pdf", 
     "name": "test.pdf", 
     "content": encodePdfText 
    } 

回答

0

最近我有同樣的問題,請嘗試以下代碼,確保httpResponse.text是UTF8 ...

"content": new Buffer(httpResponse.text.toString('base64')).toString() 
相關問題