Last active
August 29, 2015 14:12
-
-
Save aaronranard/fb480cb5fac3b0d8b1fe to your computer and use it in GitHub Desktop.
Angular: File Uploader
This file contains 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
$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; | |
}); | |
}; |
This file contains 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
<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> |
This file contains 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
/* | |
* 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