Skip to content

Instantly share code, notes, and snippets.

@fatmatto
Last active December 13, 2016 23:15
Show Gist options
  • Save fatmatto/bfa2b30cd8a633ed1d4fa80f258dbe8c to your computer and use it in GitHub Desktop.
Save fatmatto/bfa2b30cd8a633ed1d4fa80f258dbe8c to your computer and use it in GitHub Desktop.
Reading a file and uploading it with Marketcloud and the Javascript SDK
// Creating a new instance of the Marketcloud client
var client = new Marketcloud.Client({
public_key: 'your-public-key'
})
// File uploads requires at least user authentication
client.users.authenticate('example@mail.com', 'somepassword')
.then(function(response) {
// Auth was successful the client stored the user's auth token
})
.catch(function(error) {
// Something went wrong.
})
// This function is bound to the change of the input field
function encode() {
// The client has some util functions
// The first param here is the selector for the <input type="file" id="file_input"/> element
// The second the callback
Marketcloud.Utils.base64EncodeFileInput('#file_input', function(b64) {
client.files.create({
name: 'some_filename',
filename: 'filename.jpg',
file: b64
})
.then(function(response) {
//Upload was ok and response is a JSON object with the file information
})
.catch(function(error) {
// Upload was not ok
})
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment