Skip to content

Instantly share code, notes, and snippets.

@wvpv
Last active October 31, 2023 16:12
Show Gist options
  • Save wvpv/0cb528ec454b0ab1d2d91ed4a33ce7a6 to your computer and use it in GitHub Desktop.
Save wvpv/0cb528ec454b0ab1d2d91ed4a33ce7a6 to your computer and use it in GitHub Desktop.
Content Builder File Upload
<script runat="server" language="ampscript">
set @processingPageURL = "https://YOURTENANTHERE.pub.sfmc-content.com/YOURPATHHERE"
</script><!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title></title>
<style>
body {
font-size: 13px;
font-family: sans-serif;
}
</style>
</head>
<body>
Please select a file to upload:
<br><input id="file" type="file" accept="image/*">
<br><br><button id="button">Upload</button>
<div id="responseMessage"></div>
<script>
document.getElementById("button")
.addEventListener("click", function() {
var files = document.getElementById("file").files;
if (files.length > 0) {
encodeFile(files[0]);
}
});
function encodeFile(file) {
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function() {
//prepare data to pass to processing page
var rawImage = reader.result;
var base64EncodedImage = rawImage.split(";base64,")[1];
var fullFileName = document.getElementById("file").files[0].name;
var fileNameRoot = fullFileName.split(".")[0];
var fileExtension = fullFileName.split(".")[1];
fetch('%%=v(@processingPageURL)=%%', {
method: 'POST',
headers: {'Content-type': 'application/json; charset=UTF-8'},
body: JSON.stringify({
base64EncodedImage: base64EncodedImage,
fullFileName: fullFileName,
fileNameRoot: fileNameRoot,
fileExtension: fileExtension
})
}).then(function (response) {
// successful reply
if (response.ok) {
return response.json();
}
return Promise.reject(response);
}).then(function (data) {
// successful response
console.log(data);
responseMessage.innerHTML = "Got it!";
}).catch(function (error) {
// there was a problem
console.warn(error);
responseMessage.innerHTML = "Oops!";
});
};
reader.onerror = function(error) {
console.log(error);
};
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment