Permalink
Browse files

Safari appears to only support individual uploads at one time.

Better error handling.
  • Loading branch information...
1 parent b1a9dd3 commit acd03cec3a48b268517349e9dad4e3f1d088ebaf Nathan Findley committed Dec 9, 2011
Showing with 29 additions and 11 deletions.
  1. +25 −10 js/jquery.uploader.js
  2. +4 −1 upload.html
View
@@ -21,9 +21,11 @@
$.fn.uploader = function(params) {
var error = {
/* server or xhr error */
- 'type': '',
+ 'type' : '',
+ /* xhr requests status on error */
+ 'status': 0,
/* server json response or xhr object */
- 'data': {}
+ 'data' : {}
};
var settings = {
/* upload post url */
@@ -82,9 +84,8 @@
'post_upload_callback': null,
/* called when the server returns an error, when xhr fails, or on abort
- * function my_error_callback(type, object)
- * type : "server" OR "xhr"
- * object: server side json encoded error message OR the session object
+ * function my_error_callback(object)
+ * object : error object [type, status, data]
* */
'error_callback': null,
@@ -134,6 +135,13 @@
var opt = this.data('upload');
if(!opt) {
var $this = this;
+ /* multiple file upload in safari appears to be broken */
+ var ua = navigator.userAgent.toLowerCase();
+ var chrome = /chrome/.test(ua);
+ var safari = /safari/.test(ua);
+ if(safari && !chrome) {
+ $this.removeAttr("multiple");
+ }
settings.xhr = new XMLHttpRequest();
/* the change event triggers new uploading */
this.bind("change", function() {
@@ -147,6 +155,7 @@
this.data('upload', settings);
return this;
},
+ /* set up the xhr object for processing files */
'prepare_xhr' : function($this, settings) {
/* progress bar */
settings.xhr.upload.addEventListener("progress", function(event) {
@@ -166,9 +175,14 @@
return;
}
if(this.status != 200) {
- /* handle error or abort */
- error.type = 'xhr';
- error.data = settings;
+ if(this.status == 0) {
+ /* handle abort */
+ error.type = 'xhr';
+ } else {
+ error.type = 'server';
+ }
+ error.status = this.status;
+ error.data = settings;
settings.error_callback(error);
} else {
/* upload complete */
@@ -180,8 +194,9 @@
settings.post_upload_callback(headers, response);
} else {
/* handle server side errors */
- error.type = 'server';
- error.data = response;
+ error.type = 'server';
+ error.status = this.status;
+ error.data = response;
if(settings.error_callback != null) {
settings.error_callback(error);
}
View
@@ -19,6 +19,9 @@
/* do what you need to do here :) */
if(error.type === 'server') {
/* error.data will be whatever json object you returned from your server */
+ $("#" + error.data.upload_prefix + error.data.display_index).
+ text("Server Error: " + error.status).
+ removeClass('uploading').addClass('aborted');
}
}
function pre_upload_callback(file, id) {
@@ -82,7 +85,7 @@
<p>Visit <a href="http://4ydx.com/">http://4ydx.com</a> for details and
<a href="https://github.com/4ydx">https://github.com/4ydx</a> for code.</p>
<form id="uploader_form">
- <input id="uploader" type="file" multiple />
+ <input id="uploader" type="file" multiple="multiple" />
</form>
<div>
<button class="abort"

0 comments on commit acd03ce

Please sign in to comment.