0

HTML:

<div class="container">
    <div class="accordion">
        <lightning-accordion active-section-name="DocumentsFromSeller">
            <lightning-accordion-section name="DocumentsFromSeller" label="Documents From Seller">
                <template for:each={certificates} for:item="certificate">
                    <lightning-card key={certificate.name} >
                        <div class="file-upload">
                            <label >
                                {certificate.uploadLabel}
                            </label>
                            <input type="file" id={certificate.name} accept={acceptedFormats} onchange={handleFileChange} data-certificate-name={certificate.name} />
                        </div>
                    </lightning-card>
                </template>
            </lightning-accordion-section>
        </lightning-accordion>

JS:

import { LightningElement, api } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';

export default class FileUploadFromSellerComponent extends LightningElement {
    @api recordId;

    certificates = [
 ];

    get acceptedFormats() {
        return ['.pdf', '.docx'];
    }

    handleFileChange(event) {
        const fileInput = event.target;
        const files = fileInput.files;
        const maxFileSize = 2 * 1024 * 1024; // 2 MB in bytes

        const certificateName = fileInput.dataset.certificateName;
        const certificate =
            this.certificates.find((cert) => cert.name === certificateName) ||
            this.certificates2.find((cert) => cert.name === certificateName);

        for (let i = 0; i < files.length; i++) {
            const file = files[i];
            const fileSize = file.size || (file.documentId && file.documentId.size);

            if (certificate && fileSize) {
                certificate.uploadedFiles = certificate.uploadedFiles || [];

                if (fileSize <= maxFileSize) {
                    certificate.uploadedFiles.push(file);
                    this.dispatchEvent(
                        new ShowToastEvent({
                            title: 'Success',
                            message: 'File "' + file.name + '" uploaded successfully.',
                            variant: 'success',
                        })
                    );
                } else {
                    // Clear the invalid file from the input
                    fileInput.value = '';
                    this.dispatchEvent(
                        new ShowToastEvent({
                            title: 'Error',
                            message: 'File "' + file.name + '" exceeds the 2MB limit and was not uploaded.',
                            variant: 'error',
                        })
                    );
                }
            }
        }
    }
}

Hi all, I have made a custom component to upload file. Here my query is if I upload a file it gonna save in "FILES". I have many Leads and this component is added for record page if I attach a file it uploads in file content, can we make a field which holds the attached pdf/doc link to open is it possible to do?

1 Answer 1

0

Yes.You can create a custom field and have the link stored.

First you will have to understand how ContentDistribution works.Click here for the documentation Steps:

  1. Create your contentversion for you file.
  2. create your content documentlink.
  3. create the contentdistribution
  4. take the DistributionPublicUrl of the contentdistribution store it in the custom field.

You must log in to answer this question.

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