Skip to content

Instantly share code, notes, and snippets.

@aaronranard
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronranard/fb480cb5fac3b0d8b1fe to your computer and use it in GitHub Desktop.
Save aaronranard/fb480cb5fac3b0d8b1fe to your computer and use it in GitHub Desktop.
Angular: File Uploader
$scope.onFileSelect = function($files) {
// https://github.com/danialfarid/angular-file-upload
var file = $files[0];
$scope.upload = $upload.upload({
url: apiEndpoint + '/api/file/upload',
// method: 'POST' or 'PUT',
// headers: {'header-key': 'header-value'},
// withCredentials: true,
//data: {myObj: $scope.myModelObj},
file: file, // or list of files: $files for html5 only
}).success(function(data, status, headers, config) {
// file is uploaded successfully
$scope.image = data;
});
};
<div class="form-group">
<label for="file_logo">Logo</label>
<input type="file" id="file_logo" ng-file-select="onFileSelect($files)">
<p class="help-block">
<img ng-src="/{{image.path}}" /><br />
The image to show upon login.<br /><strong>200 x 170</strong>
</p>
</div>
/*
* Dependency:
* http://registry.autopergamene.eu/package/ahirarge-bookcase
*/
Route::post('api/file/upload', function(){
try {
// Trying upload
$result = Bookcase::upload(array(
'input' => 'file'
));
if($result->type == "zip"){
// For files other than images
$zip = new ZipArchive;
$res = $zip->open($result->path);
if ($res === TRUE) {
$zip->extractTo(public_path().'/excel/imports');
$media = Media::create(array(
'type' => $result->type,
'path' => 'excel/imports/'.$zip->getNameIndex(0)
));
$zip->close();
unlink($result->path);
return $media;
}
}
// Showing results
$media = Media::create(array(
'type' => $result->type,
'path' => $result->path
));
return $media;
} catch (Exception $e) {
return 'Error: ' . $e->getMessage();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment