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
aroyalaroyal 

How can i save a contact with attachment using force.com sites

Hi,

 

    I need to save a contact with attachment using force.com sites, but it save while i am using visual page, but i cant    save with attachments while using sites?  with no error.

   

    please find the solution.

   

    Here is the code:

 

    Visual Force:

 

    <apex:page controller="UploadAttachmentController"> 
    <apex:form id="theForm" forceSSL="true">
      <apex:pageBlock >
      <apex:pageBlockSection >
      <apex:inputFile id="f1" value="{!docBlob}"  fileName="{!contact.name}" />
 
          <apex:inputText id="c1" value="{!contact.LastName}" />
          <apex:commandButton action="{!CommitFile}" value="save" id="com2"/>
      </apex:pageBlockSection>
       </apex:pageBlock>
    </apex:form>
     <apex:listViews type="contact"/>
     <apex:listViews type="document" >
     </apex:listViews>
</apex:page>

 

 

Apex Class:

 

 

public class UploadAttachmentController {
    public PageReference save() {
        return null;
    }


    public String docBlob { get; set; }

    public String getAttachment() {
        return null;
    }


Contact contact;
    public Contact getContact() 
    {
    if(contact == null) contact = new Contact();
    return contact;
    }    
  public PageReference CommitFile() 
  {   
        //Contact contact = new contact(); 
        contact.lastname = contact.lastname;
        insert contact;
        Id id = System.currentPageReference().getParameters().get('id'); 
       
       
        Attachment attachment = new Attachment(); 
        PageReference Pg = new PageReference('https://na6.salesforce.com/p/attach/NoteAttach');
        //attachment.Body = Pg.getContent(); 
        Blob docBlob = Pg.getContent();
        attachment.Body = docBlob ;
        attachment.Name = contact.name;
        attachment.ParentId = contact.id;
        
        insert attachment;
        
        return null;
    }

}

 

  

Best Answer chosen by Admin (Salesforce Developers) 
BulentBulent

You can you expose visualforce pages via force.com sites (there are 3 exception pages, home, search and lookup for ideas use cases)

 

If I understand correctly you are trying to show the standard attach a file page via sites which is not a visualforce page

 

I would suggest you create your own visualforce page for this and extend the standard controller to do the attachment and displaying your visualforce page after the save action. Take a look at the example in developer book chapter 14

All Answers

Richie DRichie D

Hi,

 

I don't know  what the problem/solution is but the line:-

 

 

PageReference Pg = new PageReference('https://na6.salesforce.com/p/attach/NoteAttach');

 

 will need changing. To get anything off na6.salesforce.com you'll need to be logged in (ie. have a valid salesforce session). This is won't be the case in sites. I'm not even sure you will be able to access the attachment functionality within sites at all. 

 

I'd guess that there maybe some limitations on submitting files to a shared environment from a public site to stop 'denial of service' attacks and generally filling up your allocation of space for binary files so it may not be possible. Would be good to see if anybody can come up with a solution though...

 

R. 

 

 

 

 

aroyalaroyal

ya, i got the solution

 

thanks Richie

 

I modified the code , its works fine.

 

 

PageReference Pg = new PageReference('/apex/Page name');

 

 

Now i am storing attachments with contacts in salesforce DB using sites , but i cant view that file , it prompts the error

"Unable to access the Page" but i am having admin rights and view all data permisions.

 

I cant find the error.

 

BulentBulent

You can you expose visualforce pages via force.com sites (there are 3 exception pages, home, search and lookup for ideas use cases)

 

If I understand correctly you are trying to show the standard attach a file page via sites which is not a visualforce page

 

I would suggest you create your own visualforce page for this and extend the standard controller to do the attachment and displaying your visualforce page after the save action. Take a look at the example in developer book chapter 14

This was selected as the best answer