Project, you often need to use the file upload and download, The upload and download feature is actually an insert and query operation on the Document object. This presentation is a simple file upload and download, in theory the ID should be stored as the field of the action table after uploading, here only the presentation file is uploaded to the document object.
I. File Upload function
Apex Code
1 PublicWith sharingclassFileuploadusedtransientcontroller {2 3 Public transientBlob Fileuploadbody{get;set;}4 5 PublicString Fileuploadname{get;set;}6 7 Public voidUploadFile () {8Document uploadfiledocument =NewDocument ();9Boolean Needinsert =false;Ten if(Fileuploadbody! =NULL&& fileuploadbody.size () > 0) { OneUploadfiledocument.body =Fileuploadbody; ANeedinsert =true; - } - if(Fileuploadname! =NULL) { theUploadfiledocument.name =Fileuploadname; -Needinsert =true; - } - + if(Needinsert) { - Try { +Uploadfiledocument.folderid = ' 00528000002JyclAAC '; A Insert uploadfiledocument; atApexpages.addmessage (NewApexpages.message (ApexPages.severity.INFO, ' upload succeeded ')); -}Catch(dmlexception e) { -Apexpages.addmessage (NewApexpages.message (ApexPages.severity.ERROR, ' upload failed ')); - } -}Else { -Apexpages.addmessage (NewApexpages.message (ApexPages.severity.WARNING, ' no upload content ')); in } - } to}
Here the Blob object is used to bind the foreground value value of Inputfile, because the VF page allows memory 135K maximum, so the Blob object declares the transient type. If you upload a file that is more than 135K and click Save,
The Blob object does not declare transient or the Blob object is not set to NULL after insert, the page will be more than 135K and the page will crash.
Corresponding VF code:
1 <Apex:pageController= "Fileuploadusedtransientcontroller">2 <apex:pagemessages/>3 <Apex:form>4 <Apex:inputfilevalue= "{!fileuploadbody}"FileName= "{!fileuploadname}"/>5 <Apex:commandbuttonvalue= "Upload"Action= "{!uploadfile}"/>6 </Apex:form>7 </Apex:page>
Operating Effect:
1. Click the Upload button when you have no choice
2. Click the Upload button after selecting the file
The above code is just a demonstration of the most basic upload functionality, usually a sobject in the project creates a field to store the ID information of the document, and the ID of the document is stored in the Sobject field after the Insert upload document.
Two. page Download function
File upload naturally has a file download or file Preview function, the project usually has a field in Sobject to hold the ID of the document, that can be directly through the record to obtain the corresponding document ID. SFDC provides a way to download related document resources through a servlet, accessed in Host/servlet/servlet. Filedownload?file= ' + DocumentID
This simulates a feature page that implements the download by passing the DocumentID parameter.
Apex Code:
1 PublicWith sharingclassFiledownloadcontroller {2 3 PublicString DocumentID = ' ';4 5 PublicFiledownloadcontroller () {6 init ();7 }8 9 PublicBoolean Showdownload{get;set;}Ten One PublicFiledownloadcontroller (Apexpages.standardcontroller Controller) { A init (); - } - the Public voidinit () { -Map<string,string> Parammap =apexpages.currentpage (). GetParameters (); - if(Parammap.get (' DocumentID ')! =NULL) { -DocumentID = Parammap.get (' DocumentID ')); +Showdownload =true; -}Else { +Showdownload =false; A } at } - - PublicString downloadurl{ - get { -String urlbase = '/servlet/servlet. Filedownload?file= ' +DocumentID; - returnURLBase; in } - } to}
The corresponding VF page is as follows:
1 < apex:page controller = "Filedownloadcontroller" > 2 value = "{!downloadurl} " rendered =" {!showdownload = true} " > download </ apex:outputlink > 3 </ apex:page >
Operating effect:
1. No documentid condition in Parameters
2. There are documentid in the parameters, click Download to download the document resource corresponding to this ID.
Summary: This is just a simple description of the file upload download function, when uploading note blob object if the Inputfile in the foreground, be careful to use the transient declaration or insert after the value is set to empty OK. If there is a description of the wrong place welcome criticism, if there is a question welcome message.
Salesforce 0 Basic Learning (42) Simple File upload download