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
Simon ReparSimon Repar 

POST a file to VisualForce page

Hi everyone.

I know that it is possible to POST data (text) to VF page and than fetch them through ApexPages.getCurrentPage().getParameters(), but I didn't find a way to do the same if I POST a file and data is multipart/form-data encoded.

This is my piece of sample code:

VF page (page which receive a file after POST)
<apex:page controller="PostFile_Controller" sidebar="false" showheader="false"  applyBodyTag="false" applyHtmlTag="false" cache="false" action="{!react}">

</apex:page>
Controller:
public class PostFile_Controller {

    public PageReference react() {
	
		//get all parameters
		system.debug(ApexPages.currentPage().getParameters());
		
		//get all headers
		system.debug(ApexPages.currentPage().getHeaders());
	}	
}

VF page which POST a file (this is just an example, I actually use jquery file upload plugin (http://blueimp.github.io/jQuery-File-Upload/)):
var formData = new FormData($('form')[0]);
$.ajax({
	url: '/apex/postfile',  //Server script to process data
	type: 'POST',
	xhr: function() {  // Custom XMLHttpRequest
		var myXhr = $.ajaxSettings.xhr();
		return myXhr;
	},
	// Form data
	data: formData
});


Do you have any ideas what I am doing wrong or it is completely impossible to achieve that? 

Thank you.
bob_buzzardbob_buzzard
Is there a reason why you can't use the <apex:inpufFile /> standard component for this?  That takes all the pain away for you. Failing that its probably worth reading the following article by my fellow MVP Peter Knolle - its based on lightning components but should be straightforward to adapt for VF:

http://peterknolle.com/file-upload-lightning-component/
Simon ReparSimon Repar
Yes, there is a reason. We are using reverse proxy server which is denying uploading the file using a standard way (<apex:inputFile value="{!att.body}"/> and we didn't succeed in solving that problem on proxy. That is the reason, why I am trying to upload a file using an alternative way. HTML5 file api does not suffice, because we have bunch of users using IE 9...

Thank you Bob for the link, I will take a look.
Simon ReparSimon Repar
Well, I had a quick look to that link and find out this lightning component is using HTML5 file api as well.
Mark AllertonMark Allerton
Hi Simon,

Did you ever manage to find a solition for POSTing a file to a Visualforce page? I have a third party who needs to send a CSV file to our SF Org and their only option to use a HTTP POST so the <apex:inpufFile /> method isn't an option for me.

Thanks

Mark
 
Simon ReparSimon Repar
Hi,

no, I didn't find a solution to that problem, but I managed to implement solution which includes FileReader API (which is HTML 5 technology) and I sacrificed compatibility for IE <=9. 

So the process would be:
1. reading a file through File/FileReader API
2. implementing REST webservice for accepting String data type (if you need ws public you must expose that controller to sites).
3. sending a file to REST ws with javascript and encoding accordingly (base64 stuff).

Hope it helps.