3

I have a LWC that, is retrieving the body of an attachment using apex. This is retrieved just as blob, because handling it in Apex would hit limits such as String too long os Heap Size exceeded, cause the attachment may be 50MB, so to handle it I am using atob function in my LWC JS Controller.

Reading it is working just fine, but I want to be able to edit it as well, and I am afraid of facing any limit, like heap size, with a method like this one.

@AuraEnabled
public static void upsertAttachment(Attachment att) {
    upsert att;
}

Is this even possible?

I think that basically will leave no time for the heap size limit checker to run, so nothing will be detected, but I do not want to run into that issue if I am wrong.

Is there any good alternative for this?

We are an ISV and this code will be managed, in the past we were using Visualforce and Static resource leveraging AJAX toolkit, but I think locker service will no longer allow this, and we may run into problems with the security review

2 Answers 2

7

There's a maximum payload for LWC components (~4MB last I checked), and there's also a maximum String size, clamped at Limits.getLimitHeapSize() (so, 6MB for an AuraEnabled method); you'd get away with not much more than a 4.5MB file even if there was a larger payload limit, since you'd have to base64 encode the file data. You might be able to use the Lightning Data Service createRecord() function directly. The documentation is not clear if that will actually work (it makes no mention of Blob data types), but it'd be your best bet. Also, you could directly use lightning-file-upload, which I know specifically supports up to 2GB files.

4
  • 1
    Unfortunately, Lightning Data Service faces a "Communication Error" when trying to create a new ContentVersion with big VersionData. About lightining-file-upload I dont think I can use it, since I am displaying the content of the file (big json) in a datatable, and based of user operations, I update that attachment, programatically Commented Apr 20, 2021 at 15:58
  • @GabrielSerranoSalas I think you'd need to use Insert or Update Blob Data. Not sure if you can access this API directly in LWC, though, you might need Visualforce to help out.
    – sfdcfox
    Commented Apr 20, 2021 at 17:06
  • 1
    I managed to do callouts from the LWC js with the fetch method to some dummy endpoints, however, when trying to do the callout to my own org I am facing "blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource". I think I am near of achieving it... but it isn´t a way to overcome this problem? So far, no luck Commented Apr 21, 2021 at 6:57
  • 1
    I figured out, needed to whitelist self org URL in setup > CORS. Commented Apr 21, 2021 at 7:10
2

There is a workaround approach for uploading Files, which involves using an Iframe which would open a Visualforce page and will upload the file using Ajax toolkit, all the communications are done through postmessage API.

Lockerservice does allow the ajax toolkit, I'm not very sure about the security review, but if there's no alternative they might still approve.

Please find my full response with example code in below question: How to upload larger size file using input tag of type html in LWC

Another approach could be to hit the api from LWC directly using fetch API.

And a well heard approach is to use Chatter Rest Api to upload files upto 2GB but I have not had any hands on yet on that so you might need to do some research there.

2
  • 1
    I wanted to avoid using an Iframe since its kind of a hacky thing too. Right now I am using fetch API and am able to do it, but still I need a sessionId that I get from a VF page with another hacky approach. About using Chatter Rest API, its still an API, so I would probably need to use fetch as well as I am doing right now. My major concern is about being able to pass security review with any of these hacky approaches Commented Apr 22, 2021 at 9:29
  • There needs to be a proper way to do it, right now files are something that need these kind of workarounds since there's no straight path to follow Commented Apr 22, 2021 at 21:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .