2

I have lwc component like:

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

  }
}

and its html template is:

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

And apex class like this:

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

What I need is someway by which we can directly connect attach in apex class above to the template so that I can directly get the blob to the attach in apex class like we have for visualforce page input file component below

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

Here we directly assign the file selected to attach.body, Similarly is there any by which we can directly bind attach to lwc so that blob is available in attach.Body. This would help me in uploading large attachments like upto 25MB without hitting the heap size issue with apex.

Any help would be greatly appreciated. Thanks in Advance.

Please note: Since I am using lighning out, lightning-file-upload cant be used here

  • you can use ConnectApi's Rest API . It supports 2GB – Ysr Shk Aug 20 at 17:39
1

You'll have to use some sort of API. Apex has a 6MB heap limit, so you'd run into it regardless (binding directly wouldn't help), and also there's a maximum payload size (4MB), so the largest file you'd reasonably upload is around 3MB, include base64 encoding.

| improve this answer | |
  • Hi @sfdcfox Thanks, I tried uploading attachments using connection.js. I was able to upload 25MB. But only issue I am facing now is I have to make this available for site guest user. Since guest user don't have an API session ID I cant use connection.js. With APEX we have 4MB payload limit as you said. Can you please suggest me any alternative or workaround ? – AdityaHM Sep 10 at 16:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.