salesforce 零基礎學習(四十二)簡單檔案上傳下載

來源:互聯網
上載者:User

標籤:

項目中,常常需要用到檔案的上傳和下載,上傳和下載功能實際上是對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了。如果篇中有描述錯誤的地方歡迎批評指正,如果有問題的歡迎留言。

 

salesforce 零基礎學習(四十二)簡單檔案上傳下載

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.