1
1
/*
2
- * jQuery File Download Plugin v1.1 .0
2
+ * jQuery File Download Plugin v1.2 .0
3
3
*
4
4
* http://www.johnculviner.com
5
5
*
@@ -16,7 +16,7 @@ $.extend({
16
16
// see directly below for possible 'options'
17
17
fileDownload : function ( fileUrl , options ) {
18
18
19
- var defaultFailCallback = function ( responseHtml , url ) {
19
+ var defaultFailCallback = function ( responseHtml , url ) {
20
20
alert ( "A file download error has occurred, please try again." ) ;
21
21
} ;
22
22
@@ -55,6 +55,17 @@ $.extend({
55
55
//
56
56
failCallback : defaultFailCallback ,
57
57
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
+
58
69
//
59
70
//a period in milliseconds to poll to determine if a successful file download has occured or not
60
71
//
@@ -115,16 +126,69 @@ $.extend({
115
126
if ( settings . failCallback != defaultFailCallback ) {
116
127
settings . failCallback ( responseHtml , url ) ;
117
128
}
118
-
129
+
119
130
} else {
120
131
121
132
settings . failCallback ( responseHtml , url ) ;
122
133
}
123
134
}
124
135
} ;
125
136
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
+
128
192
129
193
//check if the file download has completed every checkInterval ms
130
194
setTimeout ( checkFileDownloadComplete , settings . checkInterval ) ;
@@ -148,14 +212,25 @@ $.extend({
148
212
}
149
213
150
214
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 ) {
154
215
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
+ }
157
228
158
- return ;
229
+ if ( isFailure ) {
230
+ internalCallbacks . onFail ( iframeDoc . body . innerHTML , fileUrl ) ;
231
+ $iframe . remove ( ) ;
232
+ return ;
233
+ }
159
234
}
160
235
}
161
236
catch ( err ) {
@@ -171,6 +246,15 @@ $.extend({
171
246
//keep checking...
172
247
setTimeout ( checkFileDownloadComplete , settings . checkInterval ) ;
173
248
}
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
+ }
174
258
}
175
259
} ) ;
176
260
0 commit comments