0

I am trying to Preview and download document from LWC, From apex controller i am returning Map<> contains ID and Document name, and iteration that map data in html file and passing map.key value to href url, but dynamic is not populating in URL.

  <template for:each={mapData} for:item="mapkey">
                           Test: {mapkey.key}                             
                           <lightning-button-icon key={mapkey} icon-name="utility:download"  variant="bare" alternative-text="Settings"></lightning-button-icon>
                               <a key={mapkey} style="color: #000000"  href ="/sfc/servlet.shepherd/document/download/{mapkey.Key}?operationContext=S1" target="_blank" preview= "{mapkey.value}">{mapkey.value}</a>  
                       </template> 

JS Code:

    .then(data => {         
  /// var conts = data;                
  console.log('data map--->'+data);
    if(data){
    // eslint-disable-next-line guard-for-in
    for(let key in data) {
   this.mapData.push({value: data[key], key:key}); //Here we are creating the array to show on UI.
   }      
}
})

Apex Code:

 @auraenabled
/* Method will return a Map Contains Document ID and Name */
public static Map<string,string> getUrls( string searchstr){
    Map<string,string> contdocidandtitle = new Map<string,string>();
    system.debug('searchstr'+searchstr);        
    For(Document cd :[select id,folderid,name from document where folder.name =:searchstr]){
        contdocidandtitle.put(cd.id,cd.name);            
    }
    return contdocidandtitle;
}

Error is: sfc/servlet.shepherd/document/download/%7Bmapkey.Key%7D?operationContext=S1 in URL "mapkey.key" priniting instead of ID Help me with valuable suggestions.

  • hi i declared @track mapData =[]; in my JS, from my HTML Test: {mapkey.key} this line priniting resepective ID from map, but it is not populating in url binding – Rajesh P Oct 23 '19 at 12:28
  • thats because you are passing the property as a string in the href parameter, or at least thats how its interpreted as per the error message – glls Oct 23 '19 at 12:33
  • HI Thank you for your quick response, Could you please let me know what exactly should i do in my case here. Thanks in advance – Rajesh P Oct 23 '19 at 12:49
  • no - please refer to the duplicate post and the documentation. If you run into any issues, please open a new post and specify what problem you are facing alongside what you have tried to resolve. – glls Oct 23 '19 at 12:49
0

Can you try this ?

Make below changes in HTML file

<template for:each={mapData} for:item="mapkey">
                       Test: {mapkey.key}                             
     <lightning-button-icon key={mapkey} icon-name="utility:download"  variant="bare" alternative-text="Settings"></lightning-button-icon>
     <a key={mapkey} href={mapkey.url} target="_blank" preview= "{mapkey.value}">{mapkey.value}</a>  
</template> 

In JS code :

.then(data => {         
   if(data){
   // eslint-disable-next-line guard-for-in
   for(let key in data) {
    this.mapData.push({value: data[key], key:key, url : '/sfc/servlet.shepherd/document/download/' + key + '?operationContext=S1' });
   }      
  }
})
  • Thanks a lot It works like Flawlessly. – Rajesh P Oct 24 '19 at 7:23
  • Sure. can you accept this answer and mark it. – Sudarshan Oct 24 '19 at 9:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.