Permalink
Browse files

Make qq.UploadHandlerForm accept an additional `customFields` option …

…more or less like uploadHandlerXhr accepts extra header fields.
  • Loading branch information...
1 parent 7954f18 commit 9465e432dd61c6196c5e71c7f7fc69e16294a740 @markotibold markotibold committed Feb 15, 2013
@@ -667,11 +667,10 @@ var BaseFileSelectDialog = AdminRESTDialog.extend({
button: upload_button_pane[0], // connecting directly to the jQUery UI upload_button doesn't work
callbacks: {
onComplete: $.proxy(function(id, fileName, responseJSON) {
- console.log(responseJSON);
this.refresh_grid();
}, this)
},
- debug: true,
+ debug: false,
request: {
endpoint: this.get_upload_path(),
inputName: this.get_upload_fieldname(),
@@ -681,6 +680,9 @@ var BaseFileSelectDialog = AdminRESTDialog.extend({
paramsInBody: true,
customHeaders: {
"X-CSRFToken": getCookie('csrftoken')
+ },
+ customFields: {
+ "csrfmiddlewaretoken": getCookie('csrftoken')
}
}
});
@@ -103,7 +103,8 @@ qq.UploadHandlerForm = function(o, uploadCompleteCallback, logCallback) {
protocol = options.demoMode ? "GET" : "POST",
form = qq.toElement('<form method="' + protocol + '" enctype="multipart/form-data"></form>'),
endpoint = options.endpointStore.getEndpoint(id),
- url = endpoint;
+ url = endpoint,
+ extraFields = options.customFields;
params[options.uuidParamName] = uuids[id];
@@ -119,11 +120,13 @@ qq.UploadHandlerForm = function(o, uploadCompleteCallback, logCallback) {
form.style.display = 'none';
// Add csrf token to the form
- var input;
- input = document.createElement('input');
- input.setAttribute('name', 'csrfmiddlewaretoken');
- input.setAttribute('value', getCookie('csrftoken'));
- form.appendChild(input);
+ qq.each(extraFields, function(name, val) {
+ var input;
+ input = document.createElement('input');
+ input.setAttribute('name', name);
+ input.setAttribute('value', val);
+ form.appendChild(input);
+ });
document.body.appendChild(form);
return form;
@@ -214,7 +217,7 @@ qq.UploadHandlerForm = function(o, uploadCompleteCallback, logCallback) {
uploadComplete(id);
});
- log('XSending upload request for ' + id);
+ log('Sending upload request for ' + id);
form.submit();
qq(form).remove();
@@ -126,7 +126,6 @@ qq.UploadHandlerXhr = function(o, uploadCompleteCallback, logCallback) {
//NOTE: return mime type in xhr works on chrome 16.0.9 firefox 11.0a2
xhr.setRequestHeader("X-Mime-Type", file.type);
}
- console.log(extraHeaders);
qq.each(extraHeaders, function(name, val) {
xhr.setRequestHeader(name, val);
});
@@ -404,7 +403,7 @@ qq.UploadHandlerXhr = function(o, uploadCompleteCallback, logCallback) {
toSend = setParamsAndGetEntityToSend(params, xhr, file, id);
setHeaders(id, xhr);
- log('YSending upload request for ' + id);
+ log('Sending upload request for ' + id);
xhr.send(toSend);
}
@@ -415,23 +414,6 @@ qq.UploadHandlerXhr = function(o, uploadCompleteCallback, logCallback) {
* Returns id to use with upload, cancel
**/
- // helper for csrf (@YoavGivati https://github.com/valums/file-uploader/pull/240)
- getCookie: function(name) {
- var cookieValue = null;
- if (document.cookie && document.cookie != '') {
- var cookies = document.cookie.split(';');
- for (var i = 0; i < cookies.length; i++) {
- var cookie = jQuery.trim(cookies[i]);
- // Does this cookie string begin with the name we want?
- if (cookie.substring(0, name.length + 1) == (name + '=')) {
- cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
- break;
- }
- }
- }
- return cookieValue;
- },
-
add: function(file){
if (!(file instanceof File)){
throw new Error('Passed obj in not a File (in qq.UploadHandlerXhr)');
@@ -12,6 +12,7 @@ qq.FineUploaderBasic = function(o){
params: {},
paramsInBody: false,
customHeaders: {},
+ customFields: {},
forceMultipart: true,
inputName: 'qqfile',
uuidName: 'qquuid',
@@ -241,6 +242,7 @@ qq.FineUploaderBasic.prototype = {
forceMultipart: this._options.request.forceMultipart,
maxConnections: this._options.maxConnections,
customHeaders: this._options.request.customHeaders,
+ customFields: this._options.request.customFields,
inputName: this._options.request.inputName,
uuidParamName: this._options.request.uuidName,
totalFileSizeParamName: this._options.request.totalFileSizeName,

0 comments on commit 9465e43

Please sign in to comment.