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
ReplyDeleteGreat post! An advanced online microsoft excel training
ReplyDeletehelps learners master complex formulas, PivotTables, dashboards, data analysis, and automation techniques through flexible, hands-on training designed for real-world business and job-ready skills.
An aws devops full course provides complete knowledge of cloud infrastructure management and DevOps practices using AWS technologies. It explains automation pipelines, deployment strategies, and monitoring systems clearly. This aws devops full course helps students gain hands-on experience through coding projects and real-time exercises. Learners create enterprise deployment workflows for practical understanding. The course prepares learners for AWS DevOps engineer roles.
ReplyDeleteVery informative article. Tableau Training Online provides comprehensive learning opportunities for mastering dashboards, visual analytics, calculated fields, and reporting techniques. These skills are essential for professionals working in data-driven organizations.tableau online course
ReplyDelete