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
satakshisatakshi 

Uploading a logo through visualforce page

I am doing a task where i want to store a logo on a visualforce page. in this i can choose one of the image from storage space and will display it as a logo. While doing this task we need to keep in mind that we can't directly display image in visualforce page in salesforce. It should be stored at some place like in document folder in our salesforce organization. i am stuck with this code i am getting error at  this line in visualforce page <apex:inputFile value="{!Document.body}" filename="{!Document.name}" />  . here i m getting error in String.name.. plz help me to solve this


public class showimage {

    public String document { get; set; }

    public showimage() {

   
    }
 public boolean showimage{set;get;}  
        public final document t;  
   private ApexPages.StandardController stdcontroller;  
   public showimage(ApexPages.StandardController stdcontroller) {  
   t = (Document) stdcontroller.getRecord();  
     t.folderid = UserInfo.getUserId(); //this saves record in My Personal Documents  
     this.stdcontroller=stdcontroller;  
     }    
  public pagereference save(){  
   this.stdcontroller.save();// this method implements the standard save method.  
   pagereference pg=new pagereference('/apex/vfpageimageupload');  
   showimage=true;  
   return pg;  
   }   
   List<document> d;  
   public id doc{set;}  
   public id getdoc()  
   {  
   d=[select id from document order by id desc limit 1]; //gets id of document inserted last in sobject  
   return d[0].id;// returns id value of list d  
   }   
   
}



VF page:

<apex:page controller="showimage">
<apex:form>
<apex:pageBlock>
 <apex:pageBlockSection title="Upload Photo" collapsible="false" columns="1" >  
 <apex:image url="https://c.ap1.content.force.com/servlet/servlet.FileDownload?file={!doc}" height="100"    
    width="100" rendered="{!showimage}" /> /*contains the url of documents,{!doc} gives the image dynamically */  
  <apex:inputFile value="{!Document.body}" filename="{!Document.name}" />  
 <center><apex:commandButton value="Upload" action="{!save}" /></center>  
  </apex:pageBlockSection> 
  </apex:pageBlock>
  </apex:form> 
</apex:page>
Best Answer chosen by satakshi
satakshisatakshi
this one is working

VF Code:

<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>




Controller:


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;
  }

}

All Answers

Shailendra Singh ParmarShailendra Singh Parmar
I am not sure why you doing it throw Document and having heavy apex code. You can simply store logo in static resources and then refer in vf pages.
Do you need upload facility also ? as your are using apex:inputFile.. 
satakshisatakshi
i want to provide a front end where user can upload  their logo images
Shailendra Singh ParmarShailendra Singh Parmar
Ok. I think, Error is because you have getter setter for "document" which is type of String. You need to change it of type Document also give it different name like "doc" so salesforce does not get confused.
satakshisatakshi
can you tell me how?
 
satakshisatakshi
this one is working

VF Code:

<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>




Controller:


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;
  }

}
This was selected as the best answer