Business Requirement
The system must support uploading files of any size from Salesforce to the external system, as the external system does not impose any file size limitations. This ensures seamless handling of large files without restrictions, aligning with the external system's capabilities.
Current Design: The file upload process is implemented using a custom LWC component instead of Salesforce's standard file upload feature because the files are uploaded directly to an external system, not stored in Salesforce. This design ensures seamless integration with the external system's APIs and allows for additional custom logic during the upload process.
File Upload Limitations :
Heap Size Limit: Files passed from LWC to Apex are restricted by the 6 MB heap size limit, regardless of whether the Apex method is synchronous or asynchronous.
Misconception About Limits: Unlike the standard file upload feature, which supports files up to 2 GB, this design cannot handle files larger than 6 MB without chunking.
When transferring a blob file from Lightning Web Components (LWC) to Apex, or vice versa, the file size limitation increases due to the need to encode the blob in Base64. This is because:
Serialization Limitation: LWC cannot directly pass blob data to Apex, as blobs are binary, and binary data is not supported in JSON serialization (the format used for LWC-Apex communication). Encoding the blob in Base64 allows it to be safely serialized and transmitted as a string.
Size Increase: Base64 encoding increases the file size by approximately 33%. This means that the size of the original blob is effectively enlarged, potentially making it exceed the limits for data transfer between LWC and Apex, which are restricted by Salesforce's platform limits. For example, the JSON response size limit for Apex to LWC communication is 6 MB, so after Base64 encoding, the original blob size should be less (<) than 4.5 MB to stay within the limit.
Why is the upload of a 3.2 MB or 3.5 MB file itself failing when it should allow up to 4.5 MB, as per the above?
If LWC component is sending additional parameters or properties along with the file (e.g., File Metadata (FileChecksum,fileId,message,success) Details,user details, configuration settings), these are included in the response as well.When these are included with the Base64-encoded file, the overall response size can exceed the 6 MB limit if the file size is large.
In Apex, the file is converted into Hexadecimal encoding (Increases size by 100%) before sending it to the external system because the system expects all file contents to be combined into a single file. Unlike Base64, Hexadecimal allows for more efficient concatenation of multiple file parts into one, ensuring compatibility with the external system's requirements.
When calling a server-side action from a client-side controller in Lightning components, the request payload limit is 4 MB. If any greater than this size will receive an error <Error: Received exception event aura:systemError from server>
Comparison with Base64
Base64 Encoding: Increases size by ~33%.
Hexadecimal Encoding: Increases size by 100%.
The maximum HTTP request size for Apex callouts is 12 MB, so files larger than this must be split into chunks.
Debugging Steps:
No comments:
Post a Comment