Skip to content

Instantly share code, notes, and snippets.

@adtennant
Last active August 29, 2015 14:25
Show Gist options
  • Save adtennant/29365e0d80f56ccc9a5c to your computer and use it in GitHub Desktop.
Save adtennant/29365e0d80f56ccc9a5c to your computer and use it in GitHub Desktop.
<input id="file-input" type="file" />
<input id="upload-button" type="button" value="Upload" onclick="uploadFile();"/>
att.Body = (new sforce.Base64Binary(binary)).toString();
sforce.connection.create([att], {
onSuccess : function(result, source) {
if (result[0].getBoolean("success")) {
console.log("new attachment created with id " + result[0].id);
}
else {
console.log("failed to create attachment " + result[0]);
}
},
onFailure : function(error, source) {
console.log("an error has occurred " + error);
}
});
reader.readAsArrayBuffer(f);
<script type="text/javascript">
var __sfdcSessionId = '{!GETSESSIONID()}';
</script>
<script src="/soap/ajax/30.0/connection.js" type="text/javascript"></script>
var input = document.getElementById('file-input');
var filesToUpload = input.files;
for(var i = 0, f; f = filesToUpload[i]; i++) {
}
var reader = new FileReader();
// Keep a reference to the File in the FileReader so it can be accessed in callbacks
reader.file = f;
reader.onload = function(e) {
}
var att = new sforce.SObject("Attachment");
att.Name = this.file.name;
att.ContentType = this.file.type;
att.ParentId = "YOUR-PARENT-ID-HERE";
var binary = "";
var bytes = new Uint8Array(e.target.result);
var length = bytes.byteLength;
for (var i = 0; i &lt; length; i++) {
binary += String.fromCharCode(bytes[i]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment