Skip to content

Instantly share code, notes, and snippets.

@msgodf
Last active December 25, 2015 22:18
Show Gist options
  • Save msgodf/7048224 to your computer and use it in GitHub Desktop.
Save msgodf/7048224 to your computer and use it in GitHub Desktop.
Image upload control for Safari on iPad that pulls the selected image out of the control and renders it, all client side
function addUploadControl() {
if(document.getElementById("imageUpload")===null) {
var fileInputContainer=document.createElement("div");
var fileInput=document.createElement("input");
fileInputContainer.style.backgroundColor="white";
fileInputContainer.style.padding="10px";
fileInputContainer.style.borderColor="black";
fileInputContainer.style.borderRadius="10px";
fileInputContainer.style.borderWidth="2px";
fileInputContainer.style.borderStyle="solid";
fileInputContainer.style.opacity="1";
fileInputContainer.style.zIndex=1200;
fileInputContainer.style.top="700px";
fileInputContainer.style.left="0px";
fileInputContainer.style.position="absolute";
fileInputContainer.appendChild(document.createTextNode("Image"));
fileInput.style.opacity="0";
fileInput.type="file";
fileInput.id="imageUpload";
var drawImage=function(e) {
var firstImage=document.getElementById("imageUpload").files[0];
var reader=new FileReader();
reader.onloadend=function(e) {
var imgString=e.target.result;
imgString=imgString.replace("data:","data:image/jpeg;");
var existingImageElement=document.getElementById("imagePreview");
if(existingImageElement===null) {
var imgElement=document.createElement("img");
imgElement.id="imagePreview";
imgElement.style.top="800px";
imgElement.style.left="0px";
imgElement.style.width="100%";
imgElement.style.position="absolute";
existingImageElement=document.body.appendChild(imgElement);
};
existingImageElement.src=imgString;
};
var blob=firstImage.webkitSlice(0,firstImage.size);
reader.readAsDataURL(blob);
};
fileInput.addEventListener("change",drawImage);
document.body.appendChild(fileInputContainer);
fileInputContainer.appendChild(fileInput);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment