This is the basic problem in apex .So we can use html input file and java script to solve this type of issue .
Below code will help you avoid the issue .
Visual force Page
<apex:page id="pgid" controller="InputFileUsingHtmlJavaScript">
<apex:form id="frm">
<input type="file" id="fileId" name="attFile" /><br/>
<apex:actionFunction name="passToController" action="{!doAttachment}" reRender="frm">
<apex:param name="fname" value="" assignTo="{!filename}" />
<apex:param name="body" value="" assignTo="{!body}"/>
</apex:actionFunction>
<apex:commandButton value="Save" onclick="remoteLocationPost();" />
<script>
var blobfile;
function getAsText(readFile) {
var reader = new FileReader();
reader.readAsDataURL(readFile);
reader.onload = attLoaded;
}
function attLoaded(evt) {
var fileString = evt.target.result;
blobfile = fileString;
var input = document.getElementById("fileId");
var filename= input.value;
passToController(filename,blobfile);
}
function remoteLocationPost(){
var fbody= document.getElementById("fileId").files[0];
getAsText(fbody);
}
</script>
</apex:form>
</apex:page>
Apex Class
public class InputFileUsingHtmlJavaScript{
public String filename{get;set;}
public String body{get;set;}
public void doAttachment(){
Attachment at=new Attachment();
at.Name=filename;
at.Body=Blob.valueOf(body);
at.Parentid=//Needs to add your record id below which you need attachment ;
Insert at;
}
}
Below code will help you avoid the issue .
Visual force Page
<apex:page id="pgid" controller="InputFileUsingHtmlJavaScript">
<apex:form id="frm">
<input type="file" id="fileId" name="attFile" /><br/>
<apex:actionFunction name="passToController" action="{!doAttachment}" reRender="frm">
<apex:param name="fname" value="" assignTo="{!filename}" />
<apex:param name="body" value="" assignTo="{!body}"/>
</apex:actionFunction>
<apex:commandButton value="Save" onclick="remoteLocationPost();" />
<script>
var blobfile;
function getAsText(readFile) {
var reader = new FileReader();
reader.readAsDataURL(readFile);
reader.onload = attLoaded;
}
function attLoaded(evt) {
var fileString = evt.target.result;
blobfile = fileString;
var input = document.getElementById("fileId");
var filename= input.value;
passToController(filename,blobfile);
}
function remoteLocationPost(){
var fbody= document.getElementById("fileId").files[0];
getAsText(fbody);
}
</script>
</apex:form>
</apex:page>
Apex Class
public class InputFileUsingHtmlJavaScript{
public String filename{get;set;}
public String body{get;set;}
public void doAttachment(){
Attachment at=new Attachment();
at.Name=filename;
at.Body=Blob.valueOf(body);
at.Parentid=//Needs to add your record id below which you need attachment ;
Insert at;
}
}
Hi Manoj,
ReplyDeleteTo configure a Custom "View All" Button in Accounts View Related list for the Activities History.
On Click of the Button, The View should contain a List of Activities(Tasks) and a Long Text Area field from a Custom Object for each activity.
Acvitity Record Id is stored in the Custom Object Field.
Can I use a Wrapper Class and add a list/field from the Custom Object instead of the Flag? Has anyone worked on a similar requirement?
Thanks
Amit
This solution worked for me. Thanks!!!
ReplyDeletethis is not working for me ,could you help me on this please,
Deletei pasted same code and assigned record id but its not inserting any file to the record
This solution worked for me too..Cheers!! :)
ReplyDeleteI keep receiving a "Invalid Page Redirection"
ReplyDeleteI keep receiving a "Invalid Page Redirection"
ReplyDeleteI keep receiving a "Invalid Page Redirection
ReplyDeleteThank you very much for your solution.. but when I try to download the attachment (EXCEL file), it is giving file is corrupted error message.. really appreciate if you can share some idea on that
ReplyDeleteThanks
I keep receiving a "Invalid Page Redirection"
ReplyDeleteReceving invalid page redirection.. any solution
ReplyDeletebody is blank when I uploading attachment
ReplyDeleteHello Sir,
ReplyDeleteI tried to run the above give code but it is passing blank body for attachment and also getting invalid page redirection error.
Please help me out in this issue.
Hello,
ReplyDeleteGetting null value in controller filename and body.
What is max size of the file this allows?
ReplyDeleteHi
ReplyDeleteits not inserting any file, and its not getting any errors
Could you plz help me on this , why its not inserting file to attachment , i assined parentId to record id
ReplyDelete