LWCでapexクラスプロパティを直接バインドできますか?apexを使用して25MBのファイルをアップロードする場合(ヒープサイズの問題)?

Aug 21 2020

私は次のようなlwcコンポーネントを持っています:

import { LightningElement } from 'lwc';
import {saveAttachment} from '@salesforce/apex/DocumentController.saveAttachment'

export default class FileUpload extends LightningElement {

  fileSelectionHandler() {
   // some logic to get the file
  }
  
  uploadFile() {
    saveAttachment({parentId: 'a0H3I000001SsylUAC'})
    .then(() => {
     // some code here
    })
    .catch(() => {
    // some code here
    })

  }
}

そのhtmlテンプレートは次のとおりです。

<template>
  <input type="file" onchange={fileSelectionHandler}>
  <button onclick={}>Upload</button>
</template>

そして、このような頂点クラス:

public without sharing class DocumentController {

  @AuraEnabled
  public static string saveAttachment(String parentId) {
    Attachment attach = new Attachment();
    attach.Name = 'test.pdf';
    attach.ParentId = parentId;
    // attach.Body ==> this part should come from lwc

    insert attach;
    return attach.Id;
  }
}

必要なのは、上記のapexクラスのattachをテンプレートに直接接続して、下のvisualforceページ入力ファイルコンポーネントの場合と同様に、blobをapexクラスのattachに直接接続できるようにすることです。

<apex:inputfile style="" value="{!attach.body}" filename="{!attach.name}" contentType="{!attach.ContentType}"/>

ここでは、選択したファイルをattach.bodyに直接割り当てます。同様に、attachをlwcに直接バインドして、attach.Bodyでblobを使用できるようにする方法もあります。これは、apexでヒープサイズの問題にぶつかることなく、最大25MBのような大きな添付ファイルをアップロードするのに役立ちます。

どんな助けでも大歓迎です。前もって感謝します。

注意:私はライトアウトを使用しているため、lightning-file-uploadはここでは使用できません

回答

1 sfdcfox Aug 21 2020 at 00:59

なんらかのAPIを使用する必要があります。Apexには6MBのヒープ制限があるため、それに関係なく遭遇します(直接バインドしても役に立ちません)。また、最大ペイロードサイズ(4MB)があるため、合理的にアップロードする最大のファイルは約3MBで、base64を含みます。エンコーディング。