【问题标题】:JSFORCE: File is not supported message is being shown while downloading file from sfdc rest api and write on local diskJSFORCE:从 sfdc rest api 下载文件并写入本地磁盘时显示文件不受支持消息
【发布时间】:2020-10-12 05:19:33
【问题描述】:

我一直在尝试使用 JS force 为节点 js 下载图像文件,并能够在检索数据并将其转换为 base64 格式后在本地创建文件,但如果显示“文件不支持消息 " 同时能够下载具有正确数据的 javascript 类型的文件。

我在salesforce中查询知识文章的附件字段。

以下是我的查询:

SELECT Body__c, Attachment__Name__s, Attachment__ContentType__s, Attachment__Length__s, Attachment__Body__s, Id, KnowledgeArticleId, Title, UrlName FROM Knowledge__kav

我正在向文章的 Attachment__Body__s 字段发送 GET 请求。

以下是我的节点js代码:

function createFile(attachmentBody,attachmntContentType,attachmntName){
var req = {
url: attachmentBody,
method: 'GET',
headers: {
        "Content-Type": attachmntContentType
        }
    };
            
var test = conn.request(req, function(err, resp) {
    if (err) {
        console.log(err)
        } else {
                        
var fileBuffer=Buffer.from(resp, 'binary').toString('base64');
console.log('fileBuffer---  '+ fileBuffer);
fs.writeFile('./downloadedAttachments/'+attachmntName,fileBuffer,'base64', function(err){
    if (err) throw err
        console.log('File saved.')
        })
            
    }
});
}

请帮帮我。

【问题讨论】:

    标签: node.js rest salesforce workbench jsforce


    【解决方案1】:

    我能够以正确的格式成功下载文件。以下是我更新的代码:

    function createFile(knbid,attachmntName,callback) {
                        
                
            var file_here = conn.sobject('Knowledge__kav').record(knbid);
            
            file_here.retrieve(function (err, response) {
            if (err) {
                return console.error(err);
                callback(0)
                } else {
                    
                    var obj = fs.createWriteStream('./downloadedAttachments/'+attachmntName, {defaultEncoding: 'binary'})
                    //console.log('blob--'+JSON.stringify(file_here.blob('Attachment__Body__s')));
                    var stream = file_here.blob('Attachment__Body__s').pipe(obj);
                
                    stream.on('finish', function (err, result) {
                    
                        if (err)
                        console.log('not downloaded'+knbid);
                        else
                        console.log('downloaded-'+knbid);
                        
                    })
                }
            });
            }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-27
      • 1970-01-01
      • 2023-04-06
      • 2019-05-17
      • 2018-04-04
      • 2022-01-18
      相关资源
      最近更新 更多