Created
August 6, 2014 15:36
-
-
Save SethTompkins/4bdb481d4c1a2e2df94e to your computer and use it in GitHub Desktop.
Angular factory for using angular-file-upload in any controller.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// replace dentalExchangeApp with the name of your Angular App. | |
// angular-file-upload.js needs to be included on your page, | |
// and included as a dependency in your App | |
dentalExchangeApp.factory('fileUpload', function($upload) { | |
return { | |
get: function($files, callback) { | |
//https://github.com/danialfarid/angular-file-upload | |
//callback is added so that you can access the returned data inside of your preferred scope. | |
var filename = $files[0].name; | |
for (var i = 0; i < $files.length; i++) { | |
var file = $files[i]; | |
$upload.upload({ | |
url: '/res/js/tinymce/plugins/ooholoader/upload.php?name=' + file['name'], //upload.php script, node.js route, or servlet url | |
method: 'POST', | |
// headers: {'header-key': 'header-value'}, | |
// withCredentials: true, | |
data: { | |
myObj: file | |
}, | |
file: file, // or list of files: $files for html5 only | |
// fileName: 'doc.jpg' or ['1.jpg', '2.jpg', ...] // to modify the name of the file | |
/* customize file formData name ('Content-Desposition'), server side file variable name. | |
Default is 'file' */ | |
//fileFormDataName: myFile, //or a list of names for multiple files (html5). | |
/* customize how data is added to formData. See #40#issuecomment-28612000 for sample code */ | |
//formDataAppender: function(formData, key, val){} | |
}).progress(function(evt) { | |
console.log('percent: ' + parseInt(100.0 * evt.loaded / evt.total)); | |
}).success(function(data, status, headers, config) { | |
// file is uploaded successfully | |
console.log(data); | |
callback(data.filename, data.fullfilename); | |
//declare a callback function when you call fileUpload.get that will | |
//pass this data to your controller's scope. | |
}); | |
//.error(...) | |
//.then(success, error, progress); | |
//.xhr(function(xhr){xhr.upload.addEventListener(...)})// access and attach any event listener to XMLHttpRequest. | |
} | |
} | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment