Skip to content

Instantly share code, notes, and snippets.

@deltaepsilon
Created June 24, 2014 18:15
Show Gist options
  • Select an option

  • Save deltaepsilon/606e4187b5b0109cf6e2 to your computer and use it in GitHub Desktop.

Select an option

Save deltaepsilon/606e4187b5b0109cf6e2 to your computer and use it in GitHub Desktop.
Restangular is capable of file uploads! See https://github.com/mgonto/restangular/issues/420
angular.module('ClientSuccess.services').factory 'fileService', (authService, $q, $rootScope)->
service = {}
service.uploadAvatar = (file) ->
deferred = $q.defer()
apiAuth = authService.enableAuth()
query =
binary: null
id: null
postIt = () ->
formData = new FormData()
formData.append('file', file) # file is an ArrayBuffer read with fileReader.readAsArrayBuffer(file)
formData.append('name', file.name)
apiAuth.all('api/employee/myinfo').withHttpConfig({transformRequest: angular.identity}).customPOST(formData, 'avatar', undefined, {'Content-Type': undefined}).then (res)->
deferred.resolve(res)
if apiAuth.all
fileReader = new FileReader();
fileReader.onload = (e) ->
query =
arrayBuffer: e.target.result
size: e.total
type: file.type
$rootScope.$apply(postIt);
fileReader.readAsArrayBuffer(file);
return deferred.promise;
else
return false
service.uploadNoteFile = (client, note, file) ->
deferred = $q.defer()
apiAuth = authService.enableAuth()
query =
binary: null
id: null
postIt = () ->
# See https://github.com/mgonto/restangular/issues/420
# See http://www.html5rocks.com/en/tutorials/file/xhr2/
formData = new FormData()
formData.append('file', file) # file is an ArrayBuffer read with fileReader.readAsArrayBuffer(file)
formData.append('name', file.name)
apiAuth.one('api/client', client.id).one('note', note.id).withHttpConfig({transformRequest: angular.identity}).customPOST(formData, 'file', undefined, {'Content-Type': undefined}).then (res)->
deferred.resolve(res)
if apiAuth.all
fileReader = new FileReader();
fileReader.onload = (e) ->
query =
arrayBuffer: e.target.result
size: e.total
type: file.type
$rootScope.$apply(postIt);
fileReader.onloadstart = ->
console.log 'onloadstart', arguments
fileReader.onloadprogress = ->
console.log 'onloadprogress', arguments
fileReader.onerror = ->
console.log 'onerror', arguments
fileReader.readAsArrayBuffer(file);
return deferred.promise;
else
return false
return service
Copy link

ghost commented Jun 7, 2016

Any javascript version would be nice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment