Skip to content

Instantly share code, notes, and snippets.

@sunilmurali
Last active October 1, 2017 14:55
Show Gist options
  • Save sunilmurali/baa1158101a16a2cc852 to your computer and use it in GitHub Desktop.
Save sunilmurali/baa1158101a16a2cc852 to your computer and use it in GitHub Desktop.
Salesforce File upload - SOAP API
File upload from Visual force page = 10 MB limit
File uplaod via SOAP API = 25MB Limit
SOAP API File Upload:
1. jquery/kendo
2. connection.js
3. FileReader [ readAsBinaryString ]
<apex:page >
<script src="LOAD JQUERY RESOURCE" />
<script src="LOAD KENDO RESOURCE" />
<script src="../../soap/ajax/35.0/connection.js" type="text/javascript"></script>
<div class="k-content">
<input type="file" name="files" id="attachment" />
<input type="submit" value="Submit" class="k-button k-primary" id="submit" />
</div>
<script>
var NAMESPACE = "" ,
ATT_CHAR_LIMIT = 6000000 ,
ATTACHMENT = "Attachment" ;
var FileUpload = function () {
var __self = this ;
__self.selectedFile = null ;
__self.init = function () {
// initialize the sessionid for the force api
sforce.connection.sessionId='{!$Api.Session_ID}';
// initalize the kendo upload element
// you can use the default upload element too and fetch the file
$('#attachment').kendoUpload( {
select: function (e) {
__self.selectedFile = e.files[0] ;
},
multiple: false,
showFileList: true
});
} ;
__self.upload = function () {
if ( __self.selectedFile== null) return ;
// read the file with FileReader api
var reader = new FileReader () ;
reader.onload = function(e)
{
if(e.target.readyState == FileReader.DONE){
var id = "" ; // get the parentRecordID, can be a url param or whatever
__self.createAttachment (e,id) ;
}
else {
console.log ( e.message ) ;
}
}
// read the file as a binary string
reader.readAsBinaryString(__self.selectedFile.rawFile);
} ;
__self.createAttachment = function (e,recordid) {
var att = new sforce.SObject("Attachment");
att["Name"] = __self.selectedFile.name;
att["ContentType"] = __self.selectedFile.type;
att["ParentId"] = recordid;
// binary string after the file read
var binaryString = e.target.result;
// encoding to base 64 format
// check the documentation for Attachment object
var base64String = new sforce.Base64Binary( binaryString ) ;
att.Body = base64String.toString();
if ( att.Body.length > ATT_CHAR_LIMIT ) {
throw "File size Limit exceeded. Maximum file size = 25MB; Max character = 6mil" ;
}
sforce.connection.create(
[att],
{
onSuccess: function ( result , source ) {
__self.onSuccess ( result , source , ATTACHMENT) ;
} ,
onFailure : function (error , source ) {
__self.onFailure ( error , source , ATTACHMENT) ;
}
}
);
}
__self.onSuccess = function (result , source , type ) {
console.log ( result ) ;
console.log ( source ) ;
// result= [{ id:"attachmentid",success:true} ]
}
__self.onFailure = function (error , source , type) {
throw "Error creating "+ type ;
console.log ( error ) ;
console.log ( source ) ;
}
}
var uploader = new FileUpload () ;
$(document).ready(function() {
uploader.init ();
$('#submit').click(function(e){
uploader.upload () ; // use promises
})
}) ;
</script>
</apex:page>
@SagarSfdc
Copy link

Hello sunil Can you share Jquery Resource and Kendo Resource?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment