You need to sign in to do that
Don't have an account?
Nirmal9114
Need to Display the Attachment
Please Tell me..where is my mistake I not able to attach a file and display it.
VF PAGE :
<apex:page controller="AttachmentUploadController" sidebar="False">
<apex:sectionHeader title="Notes And Attachment" subtitle="Attach a File"/>
<apex:form >
<apex:pageMessages />
<apex:pageBlock title="Upload an Attachment">
<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="{!attachment.name}" id="fileName"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel value="File" for="file"/>
<apex:inputFile value="{!attachment.body}" filename="{!attachment.name}" id="file"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel value="Description" for="description"/>
<apex:inputTextarea value="{!attachment.description}" id="description"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
<apex:pageBlock >
<apex:pageBlockTable value="{!lstAttach}" var="att" >
<apex:column HeaderValue="Name" >
<apex:outputField value="{!att.name}" />
</apex:column>
<apex:column HeaderValue="Description" >
<apex:outputField value="{!att.Description}" />
</apex:column>
<apex:column HeaderValue="Last Modified Date" >
<apex:outputField value="{!att.LastModifiedDate }" />
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
CONTROLLER :
public class AttachmentUploadController {
public String rightObject { get; set; }
public String listatt { get; set; }
public String item { get; set; }
public String AccountTable { get; set; }
String parentId;
public list<Attachment> lstAttach{ get; set;}
public Attachment attachment{ get; set;}
public String getAttachmentUploadController() {
return null;
}
public AttachmentUploadController() {
attachment = new Attachment();
User UserId = [Select Id, contactid, contact.accountId from user where id =: UserInfo.getUserId() limit 1];
Id AccId=UserId.contact.accountId;
lstAttach = [Select name,description,LastModifiedDate from attachment where OwnerId =: UserInfo.getUserId() and ParentId = : AccId];
}
public PageReference upload() {
Attachment attachment = new Attachment();
attachment.OwnerId = UserInfo.getUserId();
attachment.ParentId = parentId; // Account- George P. Artner record id
attachment.IsPrivate = true;
try {
insert attachment;
} catch (DMLException e) {
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment'));
return null;
}
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Attachment uploaded successfully'));
return null;
}
}
VF PAGE :
<apex:page controller="AttachmentUploadController" sidebar="False">
<apex:sectionHeader title="Notes And Attachment" subtitle="Attach a File"/>
<apex:form >
<apex:pageMessages />
<apex:pageBlock title="Upload an Attachment">
<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="{!attachment.name}" id="fileName"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel value="File" for="file"/>
<apex:inputFile value="{!attachment.body}" filename="{!attachment.name}" id="file"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel value="Description" for="description"/>
<apex:inputTextarea value="{!attachment.description}" id="description"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
<apex:pageBlock >
<apex:pageBlockTable value="{!lstAttach}" var="att" >
<apex:column HeaderValue="Name" >
<apex:outputField value="{!att.name}" />
</apex:column>
<apex:column HeaderValue="Description" >
<apex:outputField value="{!att.Description}" />
</apex:column>
<apex:column HeaderValue="Last Modified Date" >
<apex:outputField value="{!att.LastModifiedDate }" />
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
CONTROLLER :
public class AttachmentUploadController {
public String rightObject { get; set; }
public String listatt { get; set; }
public String item { get; set; }
public String AccountTable { get; set; }
String parentId;
public list<Attachment> lstAttach{ get; set;}
public Attachment attachment{ get; set;}
public String getAttachmentUploadController() {
return null;
}
public AttachmentUploadController() {
attachment = new Attachment();
User UserId = [Select Id, contactid, contact.accountId from user where id =: UserInfo.getUserId() limit 1];
Id AccId=UserId.contact.accountId;
lstAttach = [Select name,description,LastModifiedDate from attachment where OwnerId =: UserInfo.getUserId() and ParentId = : AccId];
}
public PageReference upload() {
Attachment attachment = new Attachment();
attachment.OwnerId = UserInfo.getUserId();
attachment.ParentId = parentId; // Account- George P. Artner record id
attachment.IsPrivate = true;
try {
insert attachment;
} catch (DMLException e) {
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment'));
return null;
}
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Attachment uploaded successfully'));
return null;
}
}
Your code is correct but this needs some minor changes.
VF Page
Apex Controller code
after your code i am getting this error.
Visualforce Error
apex:inputFile can not be used in conjunction with an action component, apex:commandButton or apex:commandLink that specifies a rerender or oncomplete attribute.
Please help.