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
Martin ProkopMartin Prokop 

How to attach file using actionFunction

Hello,
Could you help me please? I want to implement attaching files to Cases in SalesForce.
 
There is recomanded way which using apex:inputFile element, but I can’t use it, because on page I have to use more rerendering scopes.
 
So I decided to implement it via standard html input and action function with parameters (where content of file is passed in parameter to apex class).
 
Visualforce:
<apex:form id="techForm" enctype="multipart/form-data">
<apex:actionFunction name="fillAttachment" action="{!formController.FillAttachment}" reRender="attachmentsBlock">
<apex:param assignTo="{!formController.handleFileContent}" name="handleFileContent" value="" />
</apex:actionFunction>
<script type="text/javascript">
function check_extension(filename) {
var files = document.getElementById('attachment').files;                     
var file = files[0];
fillAttachment(file);
}
</script> 
<input type="file" accept=".gif, .jpg, .jpeg, .tif, .tiff, .png, .pdf" onChange="check_extension(this.value)" class="hidden" value="" filename="attachment" id="attachment" />
</form>

Apex Class:
public class ContactUsController {
    public  List<Attachment> attachments { get; set; }
    public String handleFileContent { get; set; }  
public void FillAttachment() {
if(this.handleFileContent != null && this.handleFileContent.length() > 0) {
Attachment toAttach = new Attachment(body = Blob.valueOf(this.handleFileContent));
this.attachments.add(toAttach);
}
}
}

The problem of this approach is that in this way, i got „Regex too complicated“ error.
  • So, I tried to check heap size (http://blog.jeffdouglas.com/2010/08/16/managing-the-heap-in-salesforce-com/), but it is no problem.
  • I also tryied to use Transcient method, but it does not work (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_keywords_transient.htm).
  • Finnaly I tried to use future method, but it is static, so it is not possible for me to use it (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_invoking_future_methods.htm).
 
I suggest that problem is that I exceed limit of view stat (https://developer.salesforce.com/forums/?id=906F0000000AsUHIA0)
 
Can you help me?
 
To Sum Up. Is there any way, how to send file from visualforce page using javascript and actionfunction to apex class? I can not use apex:imputFile because I have to use more rerender scopes on that page. I hope that there will be some workaround.
 
(I thinking about iframe page using apex:imputFile embedded to my page which handle uploading attachments and then via timpestamp-identifiers map it to relevant Case. But I think that it is "weird" way to upload files.)
 
Thank you for your answer.

Martin
Vivek DVivek D
I understand where you might be facing issues when using apex:inputFile because the rendering will go for a toss.
The solution for that is use apex:actionRegion tag to apply rendering to only specific areas.  

Checkout actionRegion 
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_actionRegion.htm
Martin ProkopMartin Prokop
Hello, thanks for you answers,

erksfdcDev, unfortunately I cannot use connection.js because API is not enabled for Site guest user.
 
Marlo Zarate 22Marlo Zarate 22
Hi Martin, 

I know this is kinda outdated already but did you find any solution using the html <input type="file"> from JS to Apex Ctrl?