You need to sign in to do that
Don't have an account?
Raghavendra M 13
Apex Controller:
public with sharing class UploadimagesController {
@AuraEnabled
public static void getfilenamesupdated(list<string> contdocumntidlst)
{
list<ContentDocument> condoclst = new list<ContentDocument>();
condoclst = [select id,Title,FileType from ContentDocument where id in: contdocumntidlst];
for(ContentDocument cd : condoclst)
{
cd.Title = cd.Title+'_'+'reqirdextension';
}
update condoclst;
}
}
Component:
<aura:component controller="UploadimagesController" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
<aura:attribute name="contentdocumentlist" type="List"/>
<aura:attribute name="recordId" type="Id" description="Record to which the files should be attached" />
<lightning:fileUpload label="Add attachment"
multiple="true"
accept=".jpg, .png"
recordId="{!v.recordId}"
onuploadfinished="{!c.handleUploadFinished}"/>
</aura:component>
Controller:
({
handleUploadFinished: function (cmp, event) {
// Get the list of uploaded files
var uploadedFiles = event.getParam("files");
var idslst = component.get("v.contentdocumentlist");
for (var i= 0; i< uploadedFiles.length;i++)
{
idslst.push(JSON.stringify(uploadedFiles[i].documentId));
}
componenet.set("v.contentdocumentlist",idslst);
var action = component.get("c.getfilenamesupdated");
action.setParams({
contdocumntidlst : component.get("v.selectedsobjct"),
});
action.setCallback(this, function(actionResult)
{
var state = actionResult.getState();
if (state === "SUCCESS")
{
// show success message – with no of files uploaded
var toastEvent = $A.get("e.force:showToast");
toastEvent.setParams({
"title": "Success!",
"type" : "success",
"message": uploadedFiles.length+ "files has been updated successfully!"
});
toastEvent.fire();
$A.get("e.force:refreshView").fire();
// Close the action panel
var dismissActionPanel = $A.get("e.force:closeQuickAction");
dismissActionPanel.fire();
}
});
$A.enqueueAction(actionResult);
},
})
Am getting below error when am trying to rename my attachment name using standard lightning upload attachment code
Apex Controller:
public with sharing class UploadimagesController {
@AuraEnabled
public static void getfilenamesupdated(list<string> contdocumntidlst)
{
list<ContentDocument> condoclst = new list<ContentDocument>();
condoclst = [select id,Title,FileType from ContentDocument where id in: contdocumntidlst];
for(ContentDocument cd : condoclst)
{
cd.Title = cd.Title+'_'+'reqirdextension';
}
update condoclst;
}
}
Component:
<aura:component controller="UploadimagesController" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
<aura:attribute name="contentdocumentlist" type="List"/>
<aura:attribute name="recordId" type="Id" description="Record to which the files should be attached" />
<lightning:fileUpload label="Add attachment"
multiple="true"
accept=".jpg, .png"
recordId="{!v.recordId}"
onuploadfinished="{!c.handleUploadFinished}"/>
</aura:component>
Controller:
({
handleUploadFinished: function (cmp, event) {
// Get the list of uploaded files
var uploadedFiles = event.getParam("files");
var idslst = component.get("v.contentdocumentlist");
for (var i= 0; i< uploadedFiles.length;i++)
{
idslst.push(JSON.stringify(uploadedFiles[i].documentId));
}
componenet.set("v.contentdocumentlist",idslst);
var action = component.get("c.getfilenamesupdated");
action.setParams({
contdocumntidlst : component.get("v.selectedsobjct"),
});
action.setCallback(this, function(actionResult)
{
var state = actionResult.getState();
if (state === "SUCCESS")
{
// show success message – with no of files uploaded
var toastEvent = $A.get("e.force:showToast");
toastEvent.setParams({
"title": "Success!",
"type" : "success",
"message": uploadedFiles.length+ "files has been updated successfully!"
});
toastEvent.fire();
$A.get("e.force:refreshView").fire();
// Close the action panel
var dismissActionPanel = $A.get("e.force:closeQuickAction");
dismissActionPanel.fire();
}
});
$A.enqueueAction(actionResult);
},
})
In your JS controller, handleUploadFinished function is passing cmp and event i.e: handleUploadFinished: function (cmp, event)
But in your controller logic you are using component.
Change the parameter to component in handleUploadFinished function, like this:
handleUploadFinished: function (component , event)
Mark it as the best answer if it helps.
Thanks.
Thanks
Use this:
Also while working with Lightning components, you have to refresh the component 2-3 times so that the component code gets refreshed.