簡體   English   中英

Salesforce jsforce下載和base64靜態資源體轉換

[英]Salesforce jsforce download and base64 conversion of staticresource body

Javascript 新手,我一直在嘗試將靜態資源記錄主體轉換為可解析的字符串,希望有人知道如何進行?

我已使用 jsforce 庫成功檢索到 static 資源。 資源是一個csv的文件,返回結果的body屬性呈現為url即。 正文:“/services/data/v56.0/sobjects/StaticResource/resourceId/Body”

我嘗試將域和 url 放在一起並執行此操作,但什么也沒發生……沒有錯誤,沒有控制台 output; 也許是錯誤的方法?

// res.records[0].Body definitely returns the url for the body
// I have put 'domain' in place of my actual salesforce domain :)
const url =  'https://domain.lightning.force.com' + res.records[0].Body;
fetch(url)
.then(res => {console.log(res); });

超出這一點我被困住了——我一直無法弄清楚如何進一步。 關於如何從這個 url 獲得實際可用結果的任何建議,我們將不勝感激!

供將來參考的代碼示例-(不要求投票...)

async function getFile(fileId) {
  let url = '/services/data/v56.0/sobjects/StaticResource/'.concat(fileId,'/Body');
  let res = '';

  const response = await fetch(url, {
    headers: {
      'Content-Type': 'application/json',
      'Authorization' : 'Bearer '+accessToken
    }
  })
  .then((response) => {
    const reader = response.body.getReader();
    return new ReadableStream({
      start(controller) {
        return pump();
        function pump() {
          return reader.read().then(({ done, value }) => {
            // When no more data needs to be consumed, close the stream
            if (done) {
              controller.close();
              return;
            }
            // Enqueue the next data chunk into our target stream
            controller.enqueue(value);
            return pump();
          });
        }
      }
    })
  })
  // Create a new response out of the stream
  .then((stream) => new Response(stream))
  // Create an object URL for the response
  .then((response) => response.blob())
  .then((blob) => blob.text())
  // Update result
  .then((chunk) => console.log(res = chunk))
  .catch((err) => console.error(err));
}

您需要設置一個 HTTP header AuthorisationBearer {session id sometimes referred to as access token} 您將取回原始有效載荷。

看起來你得到了 REST API 結果(SOAP API 將按原樣返回 base64 編碼的東西,而不是指向實際內容的鏈接)所以希望你已經知道設置 header 並具有有效的 session id?

另請參閱我的其他答案, https://stackoverflow.com/a/60284736/313628

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM