Skip to content

Commit 05beccf

Browse files
committed
Updated to 1.2.0
1 parent 790563f commit 05beccf

File tree

2 files changed

+99
-11
lines changed

2 files changed

+99
-11
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
jQuery File Download is a cross server platform compatible jQuery plugin that allows for an Ajax-like file download experience that isn’t normally possible using the web.
2+
3+
For more information and documentation please visit:
4+
http://johnculviner.com/category/jQuery-File-Download.aspx

jquery.fileDownload.js

+95-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* jQuery File Download Plugin v1.1.0
2+
* jQuery File Download Plugin v1.2.0
33
*
44
* http://www.johnculviner.com
55
*
@@ -16,7 +16,7 @@ $.extend({
1616
// see directly below for possible 'options'
1717
fileDownload: function (fileUrl, options) {
1818

19-
var defaultFailCallback = function(responseHtml, url) {
19+
var defaultFailCallback = function (responseHtml, url) {
2020
alert("A file download error has occurred, please try again.");
2121
};
2222

@@ -55,6 +55,17 @@ $.extend({
5555
//
5656
failCallback: defaultFailCallback,
5757

58+
//
59+
// the HTTP method to use. Defaults to "GET".
60+
//
61+
httpMethod: "GET",
62+
63+
//
64+
// if specified will perform a "httpMethod" request to the specified 'fileUrl' using the specified data.
65+
// data must be an object (which will be $.param serialized) or already a key=value param string
66+
//
67+
data: null,
68+
5869
//
5970
//a period in milliseconds to poll to determine if a successful file download has occured or not
6071
//
@@ -115,16 +126,69 @@ $.extend({
115126
if (settings.failCallback != defaultFailCallback) {
116127
settings.failCallback(responseHtml, url);
117128
}
118-
129+
119130
} else {
120131

121132
settings.failCallback(responseHtml, url);
122133
}
123134
}
124135
};
125136

126-
//create a temporary iframe that is used to request the file download url
127-
var $iframe = $("<iframe style='display: none' src='" + fileUrl + "'></iframe>").appendTo("body");
137+
138+
//make settings.data a param string if it exists and isn't already
139+
if (settings.data !== null && typeof settings.data !== "string") {
140+
settings.data = $.param(settings.data);
141+
}
142+
143+
144+
var $iframe, $iframeForm;
145+
146+
if (settings.httpMethod.toUpperCase() === "GET") {
147+
148+
if (settings.data !== null) {
149+
//need to merge any fileUrl params with the data object
150+
151+
var qsStart = fileUrl.indexOf('?');
152+
153+
if (qsStart != -1) {
154+
//we have a querystring in the url
155+
156+
if (fileUrl.substring(fileUrl.length - 1) !== "&") {
157+
fileUrl = fileUrl + "&";
158+
}
159+
} else {
160+
161+
fileUrl = fileUrl + "?";
162+
}
163+
164+
fileUrl = fileUrl + settings.data;
165+
}
166+
167+
//create a temporary iframe that is used to request the fileUrl as a GET request
168+
$iframe = $("<iframe style='display: none' src='" + fileUrl + "'></iframe>").appendTo("body");
169+
170+
} else {
171+
172+
var formInputs = "";
173+
if (settings.data !== null) {
174+
175+
$.each(settings.data.split("&"), function () {
176+
177+
var kvp = this.split("=");
178+
formInputs += "<input type='text' name='" + kvp[0] + "' value='" + kvp[1] + "'/>";
179+
});
180+
}
181+
182+
$iframe = $("<iframe id='jQueryFileDownload' src='about:blank'></iframe>").appendTo("body");
183+
184+
var iframeDoc = getiframeDocument($iframe);
185+
186+
iframeDoc.write("<html><head></head><body><form method='" + settings.httpMethod + "' action='" + fileUrl + "'>" + formInputs + "</form></body></html>");
187+
188+
$iframeForm = $(iframeDoc).find('form');
189+
$iframeForm.submit();
190+
}
191+
128192

129193
//check if the file download has completed every checkInterval ms
130194
setTimeout(checkFileDownloadComplete, settings.checkInterval);
@@ -148,14 +212,25 @@ $.extend({
148212
}
149213

150214
try {
151-
//is there text content in the iframe? this means the download failed
152-
if ($iframe[0].contentWindow.document.body != null &&
153-
$iframe[0].contentWindow.document.body.innerHTML.length > 0) {
154215

155-
internalCallbacks.onFail($iframe[0].contentWindow.document.body.innerHTML, fileUrl);
156-
$iframe.remove();
216+
var iframeDoc = getiframeDocument($iframe);
217+
if (iframeDoc && iframeDoc.body != null && iframeDoc.body.innerHTML.length > 0) {
218+
219+
var isFailure = true;
220+
221+
if ($iframeForm && $iframeForm.length > 0) {
222+
var $contents = $(iframeDoc.body).contents().first();
223+
224+
if ($contents.length > 0 && $contents[0] === $iframeForm[0]) {
225+
isFailure = false;
226+
}
227+
}
157228

158-
return;
229+
if (isFailure) {
230+
internalCallbacks.onFail(iframeDoc.body.innerHTML, fileUrl);
231+
$iframe.remove();
232+
return;
233+
}
159234
}
160235
}
161236
catch (err) {
@@ -171,6 +246,15 @@ $.extend({
171246
//keep checking...
172247
setTimeout(checkFileDownloadComplete, settings.checkInterval);
173248
}
249+
250+
//gets an iframes document in a cross browser compatible manner
251+
function getiframeDocument($iframe) {
252+
var iframeDoc = $iframe[0].contentWindow || $iframe[0].contentDocument;
253+
if (iframeDoc.document) {
254+
iframeDoc = iframeDoc.document;
255+
}
256+
return iframeDoc;
257+
}
174258
}
175259
});
176260

0 commit comments

Comments
 (0)