Last active
August 29, 2015 14:25
-
-
Save adtennant/29365e0d80f56ccc9a5c to your computer and use it in GitHub Desktop.
Code snippets from: http://adtennant.co.uk/blog/2014/04/25/uploading-files-to-salesforce-with-html5-and-javascript/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<input id="file-input" type="file" /> | |
<input id="upload-button" type="button" value="Upload" onclick="uploadFile();"/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
att.Body = (new sforce.Base64Binary(binary)).toString(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
reader.readAsArrayBuffer(f); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script type="text/javascript"> | |
var __sfdcSessionId = '{!GETSESSIONID()}'; | |
</script> | |
<script src="/soap/ajax/30.0/connection.js" type="text/javascript"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var input = document.getElementById('file-input'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var filesToUpload = input.files; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for(var i = 0, f; f = filesToUpload[i]; i++) { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var reader = new FileReader(); | |
// Keep a reference to the File in the FileReader so it can be accessed in callbacks | |
reader.file = f; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
reader.onload = function(e) { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var att = new sforce.SObject("Attachment"); | |
att.Name = this.file.name; | |
att.ContentType = this.file.type; | |
att.ParentId = "YOUR-PARENT-ID-HERE"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var binary = ""; | |
var bytes = new Uint8Array(e.target.result); | |
var length = bytes.byteLength; | |
for (var i = 0; i < 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