Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fileDownload session support #70

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 54 additions & 20 deletions src/Scripts/jquery.fileDownload.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@ $.extend({
//$.fileDownload('/path/to/url/', options)
// see directly below for possible 'options'
fileDownload: function (fileUrl, options) {

//flag of download status
var isCleanedUp = false;
//provide some reasonable defaults to any unspecified options below
var settings = $.extend({

//
//get session values of the URL for Ajax
//
getStatusUrl: null,
//
//Requires jQuery UI: provide a message to display to the user when the file download is being prepared before the browser's dialog appears
//
Expand Down Expand Up @@ -100,7 +104,7 @@ $.extend({

//
//the cookie name to indicate if a file download has occured
//
//this version is't use cookie and use session values. --hacked by loveapple
cookieName: "fileDownload",

//
Expand Down Expand Up @@ -326,24 +330,36 @@ $.extend({
setTimeout(checkFileDownloadComplete, settings.checkInterval);


//this version is't user cookie to save/check status and use sesion do it. -- hacked by loveapple
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 cookie
var cookieData = settings.cookieName + "=; path=" + settings.cookiePath + "; expires=" + new Date(0).toUTCString() + ";";
if (settings.cookieDomain) cookieData += " domain=" + settings.cookieDomain + ";";
document.cookie = cookieData;

//remove iframe
cleanUp(false);

return;
}

if(isCleanedUp){
return;
}
$.ajax({
type : 'POST',
url : settings.getStatusUrl,
dataType : 'json',
}).done(
function(json) {

//has the session been written due to a file download occuring?
if(hasValue(settings.cookieValue, json.code)){
//execute specified callback
internalCallbacks.onSuccess(fileUrl);

//remove cookie
//var cookieData = settings.cookieName + "=; path=" + settings.cookiePath + "; expires=" + new Date(0).toUTCString() + ";";
//if (settings.cookieDomain) cookieData += " domain=" + settings.cookieDomain + ";";
//document.cookie = cookieData;

//remove iframe
cleanUp(false);

return;
}
}).fail(function(data) {
});

//has an error occured?
//if neither containers exist below then the file download is occuring on the current window
if (downloadWindow || $iframe) {
Expand Down Expand Up @@ -437,6 +453,8 @@ $.extend({
//}

}, 0);

isCleanedUp = true;
}


Expand All @@ -445,6 +463,22 @@ $.extend({
return '&' + htmlSpecialCharsPlaceHolders[match];
});
}

/*
* Check to see if has target string in the csv String, return true.
*/
function hasValue(targetStr, csvStr){
if(!targetStr || !csvStr){
return false;
}
var tmp = csvStr.split( ',' );
for(var i = 0; i < tmp.length; i++){
if(targetStr == tmp[i]){
return true;
}
}
return false;
}

return deferred.promise();
}
Expand Down