function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
vpmvpm 

Upload button to attach image

If user click on upload button to attach any image, it will open page like standard attach file. 

 

 

 

Rajesh SriramuluRajesh Sriramulu

Hi,

 

Try this with in form tag,

 

<apex:inputFile required="true" value="{!contentFile}" filename="{!nameFile}" />

 

Regards,

Rajesh.

vpmvpm
this my vf code:
<apex:page Controller="uploadPhoto" sidebar="false" showHeader="false">
<apex:form >
<apex:pageBlock title="Attach File" >

<h1>1.Select the File</h1>
<p>Type the path of the file or click the Browse button to find the file </p>
<apex:inputFile value="{!file}"/>
<br/>
<apex:commandButton value="Browse" action="{!loadPhoto}"/>
<br/>



<h1>2.Click the "Attach File" button.</h1>
<p>Repeat steps 1 and 2 to attach multiple files </p>
<p>(When the upload is completed the file information will appear below)</p><br/>
<apex:commandButton value="Attach File" action="{!loadPhoto}"/>
<br/>
<h1>3.Click the Done button to return to the previous page</h1>
<p>(This will cancel an in-progress upload. )</p><br/>
<apex:commandButton value="Done" action="{!savePhoto}"/>

</apex:pageBlock>
</apex:form>
</apex:page>

I don't know to call this in controller. Can u help for this?

Thanks
Rajesh SriramuluRajesh Sriramulu

Hi,

 

Try this,

 

public with sharing class FileUploadController { 
public Document document { 
get { 
if (document == null) 
document = new Document(); 
return document; 

set; 

public PageReference upload() { 
document.AuthorId = UserInfo.getUserId(); 
document.FolderId = UserInfo.getUserId(); // put it in running user's folder 
try { 
insert document; 
} catch (DMLException e) { 
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading file')); 
return null; 
} finally { 
document.body = null; // clears the viewstate 
document = new Document(); 

ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'File uploaded successfully')); 
return null; 

}

 

<apex:page controller="FileUploadController">
<apex:sectionHeader title="Visualforce Example" subtitle="File Upload Example"/>

<apex:form enctype="multipart/form-data">
<apex:pageMessages />
<apex:pageBlock title="Upload a File">

<apex:pageBlockButtons >
<apex:commandButton action="{!upload}" value="Save"/>
</apex:pageBlockButtons>

<apex:pageBlockSection showHeader="false" columns="2" id="block1">

<apex:pageBlockSectionItem >
<apex:outputLabel value="File Name" for="fileName"/>
<apex:inputText value="{!document.name}" id="fileName"/>
</apex:pageBlockSectionItem>

<apex:pageBlockSectionItem >
<apex:outputLabel value="File" for="file"/>
<apex:inputFile value="{!document.body}" filename="{!document.name}" id="file"/>
</apex:pageBlockSectionItem>

<apex:pageBlockSectionItem >
<apex:outputLabel value="Description" for="description"/>
<apex:inputTextarea value="{!document.description}" id="description"/>
</apex:pageBlockSectionItem>

<apex:pageBlockSectionItem >
<apex:outputLabel value="Keywords" for="keywords"/>
<apex:inputText value="{!document.keywords}" id="keywords"/>
</apex:pageBlockSectionItem>

</apex:pageBlockSection>

</apex:pageBlock>
</apex:form>
</apex:page>

 

Regards,

Rajesh.

 

vpmvpm
Thanks.

I got some idea from this. After click the save button i have to redirect to my detail page of custom object and uploaded image will display on this page.
Using page reference i can redirect to that detail page. Can you tell the steps?
Rajesh SriramuluRajesh Sriramulu

Hi,

 

add below code in save method and pass the object id instead of master.id.

 

PageReference ReturnPage = new PageReference('/' + Master.id);

ReturnPage.setRedirect(true);

return ReturnPage;

 

 

Regards,

Rajesh.

vpmvpm
Hi,
Without using <apex:inputFile> how can upload a file or image by clicking the "Browse" button.
Rajesh SriramuluRajesh Sriramulu

Hi,

 

U need to use <apex:inputFile> tag and in upload u need to write the above code.

 

Regards,

Rajesh.

vpmvpm
Hi
I need to attach image on my custom object. Using above code attachment stored in document object not in my custom object.
Rajesh SriramuluRajesh Sriramulu

HI,

 

If u r directly storing the image from ur computer means u can have Rich Text data type for that field

 

or

 

Image can be store from documents means --

 

Please see below links

 

http://blog.shivanathd.com/2012/12/display-image-as-custom-field-on.html

 

http://suyati.com/display-an-image-as-a-custom-field-on-salesforce/

 

https://ap1.salesforce.com/help/doc/en/salesforce_useful_formula_fields.pdf

 

I have tested the above it's working fine.

 

 

Regards,

Rajesh.