項目中,常常需要用到檔案的上傳和下載下傳,上傳和下載下傳功能實際上是對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了。如果篇中有描述錯誤的地方歡迎批評指正,如果有問題的歡迎留言。
作者:zero
部落格位址:http://www.cnblogs.com/zero-zyq/
本文歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接配接
個人下載下傳了一些相關學習的PDF檔案,如果需要下載下傳請通路百度雲 點選此處通路 密碼:jhuy
如果文章的内容對你有幫助,歡迎點贊~
為友善手機端檢視部落格,現正在将部落格遷移至微信公衆号:Salesforce零基礎學習,歡迎各位關注。