From 3f187bff1ce90df62ced13d40d4d9e8be4afecfb Mon Sep 17 00:00:00 2001 From: nuxsmin Date: Thu, 7 Nov 2013 03:57:01 +0100 Subject: [PATCH] Improved account file list. Form is no more needed and improved visual aspect. --- ajax/ajax_files.php | 2 + ajax/ajax_getFiles.php | 17 ++ css/styles.css | 8 +- inc/html.class.php | 1 + js/functions.js | 49 ++--- js/functions.php | 6 +- js/jquery.fileDownload.js | 427 ++++++++++++++++++++++++++++++++++++++ 7 files changed, 479 insertions(+), 31 deletions(-) create mode 100644 js/jquery.fileDownload.js diff --git a/ajax/ajax_files.php b/ajax/ajax_files.php index 72e98b4c..7489c223 100644 --- a/ajax/ajax_files.php +++ b/ajax/ajax_files.php @@ -143,6 +143,8 @@ if ($action == 'download' || $action == 'view') { SP_Common::wrLogInfo($message); // Enviamos el archivo al navegador + header('Set-Cookie: fileDownload=true; path=/'); + header('Cache-Control: max-age=60, must-revalidate'); header("Content-length: $fileSize"); header("Content-type: $fileType"); header("Content-Disposition: attachment; filename=\"$fileName\""); diff --git a/ajax/ajax_getFiles.php b/ajax/ajax_getFiles.php index 3d4344fe..6079dd19 100644 --- a/ajax/ajax_getFiles.php +++ b/ajax/ajax_getFiles.php @@ -53,6 +53,23 @@ if ( ! is_array($files) || count($files) === 0 ){ } ?> +
+ +
+ + +
'; + } + }); + } + + if (isOtherMobileBrowser) { + + $form = $("").appendTo("body"); + $form.hide() + .prop('method', settings.httpMethod) + .prop('action', fileUrl) + .html(formInnerHtml); + + } else { + + if (isIos) { + + downloadWindow = window.open("about:blank"); + downloadWindow.document.title = settings.popupWindowTitle; + formDoc = downloadWindow.document; + window.focus(); + + } else { + + $iframe = $("").appendTo("body"); + formDoc = getiframeDocument($iframe); + } + + formDoc.write("" + formInnerHtml + "
" + settings.popupWindowTitle + ""); + $form = $(formDoc).find('form'); + } + + $form.submit(); + } + + + //check if the file download has completed every checkInterval ms + setTimeout(checkFileDownloadComplete, settings.checkInterval); + + + function checkFileDownloadComplete() { + + //has the cookie been written due to a file download occuring? + if (document.cookie.indexOf(settings.cookieName + "=" + settings.cookieValue) != -1) { + + //execute specified callback + internalCallbacks.onSuccess(fileUrl); + + //remove the cookie and iframe + document.cookie = settings.cookieName + "=; expires=" + new Date(1000).toUTCString() + "; path=" + settings.cookiePath; + + cleanUp(false); + + return; + } + + //has an error occured? + //if neither containers exist below then the file download is occuring on the current window + if (downloadWindow || $iframe) { + + //has an error occured? + try { + + var formDoc = downloadWindow ? downloadWindow.document : getiframeDocument($iframe); + + if (formDoc && formDoc.body != null && formDoc.body.innerHTML.length) { + + var isFailure = true; + + if ($form && $form.length) { + var $contents = $(formDoc.body).contents().first(); + + if ($contents.length && $contents[0] === $form[0]) { + isFailure = false; + } + } + + if (isFailure) { + internalCallbacks.onFail(formDoc.body.innerHTML, fileUrl); + + cleanUp(true); + + return; + } + } + } + catch (err) { + + //500 error less than IE9 + internalCallbacks.onFail('', fileUrl); + + cleanUp(true); + + return; + } + } + + + //keep checking... + setTimeout(checkFileDownloadComplete, settings.checkInterval); + } + + //gets an iframes document in a cross browser compatible manner + function getiframeDocument($iframe) { + var iframeDoc = $iframe[0].contentWindow || $iframe[0].contentDocument; + if (iframeDoc.document) { + iframeDoc = iframeDoc.document; + } + return iframeDoc; + } + + function cleanUp(isFailure) { + + setTimeout(function() { + + if (downloadWindow) { + + if (isAndroid) { + downloadWindow.close(); + } + + if (isIos) { + downloadWindow.focus(); //ios safari bug doesn't allow a window to be closed unless it is focused + if (isFailure) { + downloadWindow.close(); + } + } + } + + //iframe cleanup appears to randomly cause the download to fail + //not doing it seems better than failure... + //if ($iframe) { + // $iframe.remove(); + //} + + }, 0); + } + + + function htmlSpecialCharsEntityEncode(str) { + return str.replace(htmlSpecialCharsRegEx, function(match) { + return '&' + htmlSpecialCharsPlaceHolders[match]; + }); + } + + return deferred.promise(); + } +}); + +})(jQuery, this);