Skip to content

Instantly share code, notes, and snippets.

@jacobtwlee
jacobtwlee / ProcessFile.js
Last active October 15, 2018 08:42
Processing the image with canvas
function processFile(dataURL, fileType) {
var maxWidth = 800;
var maxHeight = 800;
var image = new Image();
image.src = dataURL;
image.onload = function () {
var width = image.width;
var height = image.height;
@jacobtwlee
jacobtwlee / SendFile.js
Last active April 14, 2017 02:12
Uploading the file with AJAX
function sendFile(fileData) {
var formData = new FormData();
formData.append('imageData', fileData);
$.ajax({
type: 'POST',
url: '/your/upload/url',
data: formData,
contentType: false,
@jacobtwlee
jacobtwlee / ReadFile.js
Last active January 15, 2017 15:12
Reading the file
function readFile(file) {
var reader = new FileReader();
reader.onloadend = function () {
processFile(reader.result, file.type);
}
reader.onerror = function () {
alert('There was an error reading the file!');
}
@jacobtwlee
jacobtwlee / HandlingFileInput.js
Last active January 15, 2017 15:12
Handling file input
if (window.File && window.FileReader && window.FormData) {
var $inputField = $('#file');
$inputField.on('change', function (e) {
var file = e.target.files[0];
if (file) {
if (/^image\//i.test(file.type)) {
readFile(file);
} else {
@jacobtwlee
jacobtwlee / FileSelect.html
Last active January 15, 2017 15:12
File select
<input id="file" type="file" accept="image/*">
@jacobtwlee
jacobtwlee / UploadSupportDetection.js
Last active July 16, 2019 13:59
Upload support detection
function isUploadSupported() {
if (navigator.userAgent.match(/(Android (1.0|1.1|1.5|1.6|2.0|2.1))|(Windows Phone (OS 7|8.0))|(XBLWP)|(ZuneWP)|(w(eb)?OSBrowser)|(webOS)|(Kindle\/(1.0|2.0|2.5|3.0))/)) {
return false;
}
var elem = document.createElement('input');
elem.type = 'file';
return !elem.disabled;
};