Skip to content

Commit

Permalink
Merge pull request #53 from cillosis/custom-headers-feature
Browse files Browse the repository at this point in the history
Added ability to pass custom headers in request. Fixes issue #47.
  • Loading branch information
LPology committed Mar 12, 2014
2 parents c2bf1fa + 35e0b59 commit 165adfc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var uploader = new ss.SimpleUpload({
* Use any HTML element as the upload button
* No dependencies - use it with or without jQuery
* Provides individual callback functions for XHR-supported browsers and for browsers that do not support XHR uploads
* Ability to pass custom headers in request such as the Authorization header

### Server-side file handling ###
Files are uploaded by POST as either raw form data or regular multipart/form-data, depending on the browser.
Expand Down Expand Up @@ -130,7 +131,7 @@ var uploader = new ss.SimpleUpload({
// Do something after finishing the upload
// Note that the progress bar will be automatically removed upon completion because everything
// is encased in the "wrapper", which was designated to be removed with setProgressContainer()
onComplete: function(filename, response) {
onComplete: function(filename, response) {
if (!response) {
alert(filename + 'upload failed');
return false;
Expand Down Expand Up @@ -201,9 +202,9 @@ $Upload = new FileUpload('uploadfile');
$result = $Upload->handleUpload($upload_dir, $valid_extensions);

if (!$result) {
echo json_encode(array('success' => false, 'msg' => $Upload->getErrorMsg()));
echo json_encode(array('success' => false, 'msg' => $Upload->getErrorMsg()));
} else {
echo json_encode(array('success' => true, 'file' => $Upload->getFileName()));
echo json_encode(array('success' => true, 'file' => $Upload->getFileName()));
}
```

Expand All @@ -228,5 +229,17 @@ if ($result) {
}
```

### Passing Custom Headers ###

You can pass custom headers in the options upon initialization:

```javascript
var uploader = new ss.SimpleUpload({
customHeaders: {'Authorization': 'my-access-token'},
...
});

```

### License ###
Released under the MIT license.
6 changes: 6 additions & 0 deletions SimpleAjaxUploader.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ ss.SimpleUpload = function( options ) {
hoverClass: '',
focusClass: '',
disabledClass: '',
customHeaders: [],
onAbort: function( filename, uploadBtn ) {},
onChange: function( filename, extension, uploadBtn ) {},
onSubmit: function( filename, extension, uploadBtn ) {},
Expand Down Expand Up @@ -1135,6 +1136,11 @@ ss.SimpleUpload.prototype = {
xhr.setRequestHeader( 'Accept', 'application/json, text/javascript, */*; q=0.01' );
}

// Set custom request headers
for ( var headerName in opts.customHeaders ) {
xhr.setRequestHeader( headerName, opts.customHeaders[headerName] );
}

if ( opts.multipart === true ) {
var formData = new FormData();

Expand Down

0 comments on commit 165adfc

Please sign in to comment.