From a lightning component, If you would like to Upload a Logo/Attachments/Images into an object of Salesforce
this post would be really helpful.
After a long work around I was able to figure out how to upload attachments from outside.
Using the below code, you can directly attach the file and upload into object.
Component Code:
<lightning:layoutItem padding=”around-small” size=”4″>
<lightning:input aura:id=”file-input1″ type=”file”
files=”{!v.fileToBeUploaded2}”
accept=”image/png, image/jpeg”
onchange=”{!c.onFileSelected2}”
label=”Logo 2″
name=”file1″ multiple=”false”/>
<div>
File Selected: {!v.uploads.fileToBeUploaded2Name}
</div>
</lightning:layoutItem>
Controller
onFileSelected1:function(component,event,helper){
let files = component.get(“v.fileToBeUploaded1”);
if (files && files.length > 0) {
let file = files[0][0];
let uploads = component.get(“v.uploads”);
uploads[“fileToBeUploaded1Name”] = file.name;
component.set(“v.uploads”,uploads);
}
Helper
let uploads = component.get(“v.uploads”);
uploads.success = [];
component.set(“v.uploads”,uploads);
There is also a standard lightning design system tag that can be used in the lightning component.But, the limitation is that
It can only be used internally. If you would like to upload images from public facing forms we can use the custom code i have given.
File Upload
Component
<aura:component>
<aura:attribute name=”accept” type=”List” default=”[‘.jpg’, ‘.jpeg’]”/>
<aura:attribute name=”multiple” type=”Boolean” default=”true”/>
<aura:attribute name=”disabled” type=”Boolean” default=”false”/>
<lightning:fileUpload name=”fileUploader”
label= “Demo Upload”
multiple=”{!v.multiple}”
accept=”{!v.accept}”
disabled=”{!v.disabled}”
recordId=”abcd”
onuploadfinished=”{! c.handleUploadFinished }”/>
</aura:component>
Controller
({
handleUploadFinished: function (cmp, event) {
// This will contain the List of File uploaded data and status
var uploadedFiles = event.getParam(“files”);
alert(“Files uploaded : ” + uploadedFiles.length);
}
})
–Chandra V[11/11/18]