1

I have created a static resource(Name = Excel_Macro_Report) where I uploaded excel macro file with cache control as public. Then I created visualforce page with the following Visualforce Markup:

<apex:page >
 <script>
 var URL = URL.getSalesforceBaseUrl().toExternalForm()+''+PageReference.forResource(Excel_Macro_Report).getUrl();
 var win = window.open(URL, "target=_blank");
 </script>
</apex:page>

Then I created visualforce tab so that I can reference the VF page that would download the excel_macro_report into the local machine. However, when i click on the VF tab its not downloading the file and it just opens a new window with SF home page and only the tabs.

Could anyone please help me with anything I am doing wrong here or with the VFMarkup ?

1 Answer 1

1

You're mixing JavaScript and Apex code.

This line:

 var URL = URL.getSalesforceBaseUrl().toExternalForm()+''+PageReference.forResource(Excel_Macro_Report).getUrl();

Doesn't work, because the URL class is Apex, not JavaScript.

Instead, use $Resource:

window.open("{!$Resource.Excel_Macro_Report}", "_blank");
7
  • Thank you @sfdcfox.
    – bibar
    Commented Mar 28 at 16:09
  • The file gets downloaded only when I allow pop-up. By default the pop-up is set to block. Any suggestion if I need to append the code to bypass the pop-up ? I researched and tried few of them , didn't help.
    – bibar
    Commented Mar 28 at 17:48
  • @bibar are you trying to open it in a tab, or download the file? The solution will be different depending on which you would like to do.
    – sfdcfox
    Commented Mar 28 at 17:56
  • I am trying to only download the excel macro file.
    – bibar
    Commented Mar 28 at 17:58
  • @bibar In general, browser security prevents "casual" downloads and popups. This is a good thing. Instead, make it an intentional action for the user to perform, such as <a href="{!$Resource.Excel_Macro_Report}" download filename="Excel Macro Report.xlsx">Download</a>. This should allow the file to be downloaded directly without the browser requiring additional action.
    – sfdcfox
    Commented Mar 28 at 18:35

You must log in to answer this question.

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