简体   繁体   English

Visualforce页面中的文本文件下载

[英]Text file download in Visualforce page

I have a download button in my visualforce page, on clicking which a text file(.txt format) has to be downloaded. 我的visualforce页面中有一个下载按钮,点击后必须下载文本文件(.txt格式)。 This text file will be created dynamically with the data stored in a Text field of a custom object. 将使用存储在自定义对象的“文本”字段中的数据动态创建此文本文件。 Now I am struggling to acheive this simple download functionality without creating Attachments or Document Objects. 现在,我正在努力实现这种简单的下载功能,而无需创建附件或文档对象。 Is there any possible way to download content as plain text file? 有没有办法将内容下载为纯文本文件? Could some one please help me with this? 有人可以帮我这个吗?

I have tried the below visualforce code, but it is not downloading any files. 我尝试了下面的visualforce代码,但它没有下载任何文件。

<a href="data:text/plain;charset=utf-8;base64,{!getEncodedData}"> Download License </a></apex:outputLabel>

where getEncodedData will be the text file body. 其中getEncodedData将是文本文件正文。

Apex code: Apex代码:

getEncodedData = EncodingUtil.base64Encode(Blob.valueOf(strContent));

PN : I am trying to achieve this without creating Attachments, simply because the created file will not be reused later. PN:我试图在不创建附件的情况下实现这一点,因为创建的文件以后不会被重用。

Any help is really appreciated..!! 任何帮助真的很感激.. !!

Make you button open a new tab (target="_blank" if I recall correctly) and load up a new page. 让你按钮打开一个新标签(如果我没记错的话,目标=“_空白”)并加载一个新页面。 You can put together a normal page with merge fields and everything, you just need to change the content type: 您可以将包含合并字段和所有内容的普通页面放在一起,您只需要更改内容类型:

<apex:page standardController="Account" contentType="application/vnd.ms-excel">

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_styling_content_type.htm (note that there are more content types available than what they list on that page). https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_styling_content_type.htm (请注意,可用的内容类型多于他们在该页面上列出的内容类型)。

JavaScript Textdownload made it simple than the href usage, especially in this case :-) JavaScript Textdownload比href用法简单,特别是在这种情况下:-)

And here is the exact flow. 这是确切的流程。

Visualforce Code: Visualforce代码:

<apex:outputLabel onClick="javascript:fnDownloadContent('{!ID}','{!compId}');" >Download</apex:outputLabel>  
<apex:actionfunction name="actDnldContent" action="{!fileContent}" reRender="" oncomplete="javascript:download('{!filename}','{!getData}');">
<apex:param name="Id" value="" assignTo="{!Id}" />
<apex:param name="compId" value="" assignTo="{!CompId}"/>                                                                     
</apex:actionfunction>

JavaSript function: JavaSript函数:

function fnDownloadContent(ID, compID)
{
    actDnldContent(ID, compID);         
}  
function download(filename,text) 
{
    var element = document.createElement('a');
    element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
    element.setAttribute('download', filename);
    element.style.display = 'none';
    document.body.appendChild(element);
    element.click();
    document.body.removeChild(element);
}

In the above code, the 'filename' and 'getData' variables will be set on calling the Apex method 'fileContent' in the Apex Controller. 在上面的代码中,将在Apex控制器中调用Apex方法'fileContent'时设置'filename'和'getData'变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM