salesforce 零基础学习(四十二)简单文件上传下载
項目中,常常需要用到文件的上傳和下載,上傳和下載功能實際上是對Document對象進行insert和查詢操作.本篇演示簡單的文件上傳和下載,理論上文件上傳后應該將ID作為操作表的字段存儲,這里只演示文件上傳到Document對象中。
一.文件上傳功能
apex代碼
1 public with sharing class FileUploadUsedTransientController { 2 3 public transient Blob fileUploadBody{get;set;} 4 5 public String fileUploadName{get;set;} 6 7 public void uploadFile() { 8 Document uploadFileDocument = new Document(); 9 Boolean needInsert = false; 10 if(fileUploadBody != null && fileUploadBody.size() > 0) { 11 uploadFileDocument.body = fileUploadBody; 12 needInsert = true; 13 } 14 if(fileUploadName != null) { 15 uploadFileDocument.Name = fileUploadName; 16 needInsert = true; 17 } 18 19 if(needInsert) { 20 try { 21 uploadFileDocument.FolderId = '00528000002JyclAAC'; 22 insert uploadFileDocument; 23 ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.INFO,'上傳成功')); 24 } catch(DmlException e) { 25 ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.ERROR,'上傳失敗')); 26 } 27 } else { 28 ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.WARNING,'無上傳內容')); 29 } 30 } 31 }這里Blob對象用來綁定前臺的inputFile的value值,因為VF頁面最大允許內存135K,所以Blob對象聲明transient類型。如果上傳一個超過135K的文件并且點擊保存以后,
Blob對象不聲明transient或者在insert以后不將Blob對象置為null,則頁面將會超過135K,頁面會崩潰。
相應VF代碼:
1 <apex:page controller="FileUploadUsedTransientController"> 2 <apex:pageMessages /> 3 <apex:form > 4 <apex:inputFile value="{!fileUploadBody}" fileName="{!fileUploadName}"/> 5 <apex:commandButton value="上傳" action="{!uploadFile}"/> 6 </apex:form> 7 </apex:page>?運行效果:
1.什么都沒有選擇情況下點擊上傳按鈕
2.選擇文件后點擊上傳按鈕
以上代碼只是演示最基本的上傳功能,項目中通常一個sObject創建一個字段用來存儲document的ID信息,當insert上傳的Document以后將document的ID存儲在sObject的字段中。
二.頁面下載功能
文件上傳自然便有文件下載或者文件預覽功能,項目中通常在sObject中有一個字段存放Document的ID,那樣可以直接通過記錄來獲取到相應的document的ID。SFDC提供了通過servlet方式下載相關document資源,訪問方式為host/servlet/servlet.FileDownload?file=' + documentId
此處模擬通過傳遞documentId參數來實現下載的功能頁面。
apex代碼:
1 public with sharing class FileDownloadController { 2 3 public String documentId = ''; 4 5 public FileDownloadController() { 6 init(); 7 } 8 9 public Boolean showDownload{get;set;} 10 11 public FileDownloadController(ApexPages.StandardController controller) { 12 init(); 13 } 14 15 public void init() { 16 Map<String,String> paramMap = ApexPages.currentPage().getParameters(); 17 if(paramMap.get('documentId') != null) { 18 documentId = paramMap.get('documentId'); 19 showDownload = true; 20 } else { 21 showDownload = false; 22 } 23 } 24 25 public String downloadURL{ 26 get { 27 String urlBase = '/servlet/servlet.FileDownload?file=' + documentId; 28 return urlBase; 29 } 30 } 31 }?相應VF頁面如下:
1 <apex:page controller="FileDownloadController"> 2 <apex:outputLink value="{!downloadURL}" rendered="{!showDownload == true}">下載</apex:outputLink> 3 </apex:page>運行效果:
1.參數中沒有documentId情況
2.參數中有documentId情況,點擊下載后便可以下載此ID對應的document資源。
總結:本篇只是描述很簡單的文件上傳下載功能,上傳的時候注意Blob對象如果綁定前臺的inputFile情況下,要注意使用transient聲明或者insert以后將值置成空就OK了。如果篇中有描述錯誤的地方歡迎批評指正,如果有問題的歡迎留言。
?
?
轉載于:https://www.cnblogs.com/zero-zyq/p/5752978.html
總結
以上是生活随笔為你收集整理的salesforce 零基础学习(四十二)简单文件上传下载的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jexus防止产生 *.core文件
- 下一篇: 骰子的排列组合