/// $(document).ready(function(){ UploadForm(); Sys.WebForms.PageRequestManager.getInstance().add_endRequest(UploadForm); }) // Builds a single file upload form function UploadForm() { 'use strict'; var _path = '/handlers/DragNDrop/UploadHandler.ashx'; //$(location).attr('pathname') + '/UploadFile'; //console.log($(location).attr('pathname')); // Initialize the jQuery File Upload widget: $('div[id^=fileupload_]').each(function () { // mark as wired up var isWiredUp = $(this).attr('data_wiredup'); if (isWiredUp == 'true') return; else $(this).attr('data_wiredup', 'true'); // find the dropzone var _drop = $(this).attr('id').split('_')[1]; var _zone = $('#dropzone_' + _drop); // get the extra form data var _limit = undefined; if ($(this).find('input[name="limit"]').first().val() == 'true') _limit = 1; // if ($(this).find('input[type="file"]').length == 0) // $(this).append('
'); $(this).fileupload({ url: _path, // instead post back to the page and use the handler code limitMultiFileUploads: _limit, dropZone: _zone, autoUpload: true, done: DoneWithSingleFile, processData: false, add: function(e,data){ // optionally pop the progress if($(this).find('input[name="bubble"]').length > 0) $('div[id$="ctl00_upProg"]').fadeIn(); // collect inputs var payload = []; $(this).find('input').each(function(f) { var name = $(this).attr('name'); var valu = $(this).val(); if (name == 'maxup') { name = 'multiple'; valu = (valu === '0') ? 'true' : 'false'; } else if (name == 'userootpath') name = 'useroot'; else if (valu === undefined) valu = ''; payload[payload.length] = { name: name, value: valu }; }); //_mimetype = "multipart/form-data"; $.each([{ name: 'multiple', value: 'false' }, { name: 'rename', value: '' }, { name: 'height', value: '0' }, { name: 'width', value: '0' }, { name: 'mime-type', value: '' }, { name: 'extension', value: '' }, { name: 'maxsize', value: '' }, { name: 'maxspace', value: '' }, { name: 'codec', value: '' }, { name: 'prefix', value: '' }, { name: 'useroot', value: '' }, { name: 'domain', value: '0' }, { name: 'OverrideInitialNumbering', value: 'false' }, { name: 'useyoutube', value: 'false' }, { name: 'usevimeo', value: 'false' }, { name: 'title', value: '' }, { name: 'description', value: '' }], function () { var addToList = true; var that = this; $.each(payload, function() { addToList = addToList && this.name != that.name }); if (addToList) payload[payload.length] = { name: that.name, value: that.value }; }); // NOTE: this is named for conformity var usealternateprogressindicator = false; $.each(payload, function() { usealternateprogressindicator = usealternateprogressindicator || this.name == 'usealternateprogressindicator'; }); if (typeof(AlternateProgressIndicator) == 'function' && usealternateprogressindicator) AlternateProgressIndicator(this); var isVideo = false; var that = $(this); $.each(payload, function() { isVideo = isVideo || (this.name == 'mime-type' && this.value.includes('video')); }); PermissionsCheck(isVideo, data.files[0]).then(function(canUpload) { if (canUpload.canUpload) { if (isVideo && canUpload.video){ //var canvas = document.createElement('canvas'); //$(canvas).width(canUpload.video.videoWidth); //$(canvas).height(canUpload.video.videoHeight); //TODO: grab from middle by dividing canUpload.video.duration //canvas.getContext('2d').drawImage(canUpload.video, 0, 0, canUpload.video.videoWidth, canUpload.video.videoHeight); //var base64Image = canvas.toDataURL(); } data.formData = payload; data.submit(); } else { if (usealternateprogressindicator && typeof(DestroyAlternateProgressIndicator) == 'function') DestroyAlternateProgressIndicator(that); /*if (MakeToast) { $('.toast').each(function () { }); MakeToast(); }*/ } }); } }); }); // Open download dialogs via iframes, // to prevent aborting current uploads: $(document).on('click', '[id^="fileupload"] .files a:not([target^=_blank])', function (e) { e.preventDefault(); $('') .prop('src', this.href) .appendTo('body'); }); } function PermissionsCheck(isVideo, videoFile) { return new Promise(function(resolve) { var canUpload = !isVideo; var resolution = { canUpload: canUpload, video: null }; if (isVideo) { // look up maxvideos var maxDuration = 0; $('#MaxVideoDuration').each(function(e) { maxDuration = $(this).val(); }); /*window.URL = window.URL || window.webkitURL; var video = document.createElement('video'); video.preload = 'metadata'; video.onloadmetadata = function() { window.URL.revokeObjectURL(video.src); } video.src = URL.createObjectURL(data.files[0]); console.log(video.duration);*/ var reader = new FileReader(); reader.onload = function(e) { var video = document.createElement('video'); video.src = e.target.result; var timer = setInterval(function() { if (video.readyState===4) { clearInterval(timer); canUpload = video.duration < maxDuration; resolution.canUpload = canUpload; resolution.video = video; resolve(resolution); } }, 500); } reader.readAsDataURL(videoFile); } else resolve(resolution); }); } // Dragover for file upload // Kang'd https://github.com/blueimp/jQuery-File-Upload/wiki/Drop-zone-effects $(document).bind('dragover', function (e) { var dropZone = $('[id^=fileupload_]'); //$('[id^="dropzone"]'); var newTarget; var node = e.target; do { for (var i = 0; i < dropZone.length; i++) { if (node === dropZone[i]) { newTarget = node; break; } } node = node.parentNode; } while (node != null); if (newTarget == null) dropZone.find('div[id^="dropzone"]').switchClass('hover', 'nohover'); //.removeClass('hover'); else $(newTarget).find('div[id^="dropzone"]').switchClass('nohover', 'hover'); //.addClass('hover'); }); $(document).bind('drop', function (e) { $('[id^="dropzone"]').switchClass('hover', 'nohover'); //.removeClass('hover'); }); $(document).bind('drop dragover', function (e) { e.preventDefault(); }); // handles completed uploads function DoneWithSingleFile(e, data) { if (data.textStatus == "success") { // swap image /*var swap_image = $(data.dropZone).css('background-image'); if (swap_image.indexOf('?') > -1) swap_image = swap_image.substring(0, swap_image.indexOf('?')); else swap_image = swap_image.substring(0, swap_image.length - 1); swap_image += "?dummy=" + new Date().getTime() + ")"; $(data.dropZone).css('background-image', ''); $(data.dropZone).css('background-image', swap_image);*/ //$(e.target).find('div.fileupload-content > table.files tr').remove(); //IMPLEMENTATION: 2/10/2016: Unsafe Input Control support var usealternatedonewithsinglefile = false; $.each(data.formData, function() { usealternatedonewithsinglefile = usealternatedonewithsinglefile || this.name == 'usealternatedonewithsinglefile'; }); if (typeof(AlternateDoneWithSingleFile) == 'function' && usealternatedonewithsinglefile) AlternateDoneWithSingleFile(e, data); else __doPostBack($(this).attr('id'),''); //{ // if (typeof(makeAllSafe) == 'function') makeAllSafe(); // $('[id$="bCallbackPostBack"]').first().click(); //} //console.log("win!"); } }