Last active
March 20, 2017 12:50
-
-
Save cristoferdomingues/aaf142f9526a79401774f4b688f5e9f3 to your computer and use it in GitHub Desktop.
Creates a FormData and sends images to server
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
app.factory('fotosRest', function (ENV, $timeout, $cordovaFileTransfer, $q, $http, session) { | |
var _makeid = function () { | |
var text = ""; | |
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; | |
for (var i = 0; i < 5; i++) | |
text += possible.charAt(Math.floor(Math.random() * possible.length)); | |
return text; | |
}; | |
var _dataURItoBlob = function (dataURI) { | |
console.log('dataURI', dataURI.split(',')); | |
var byteString = atob(dataURI.split(',')[1]); | |
var ab = new ArrayBuffer(byteString.length); | |
var ia = new Uint8Array(ab); | |
for (var i = 0; i < byteString.length; i++) { | |
ia[i] = byteString.charCodeAt(i); | |
} | |
return new Blob([ab], { | |
type: 'image/jpeg' | |
}); | |
}; | |
var _blobToFile = function (theBlob, fileName) { | |
theBlob.lastModifiedDate = new Date(); | |
theBlob.name = fileName; | |
return theBlob; | |
}; | |
var _getFileToUpload = function (files) { | |
var convertedFiles = []; | |
files.forEach(function (file) { | |
convertedFiles.push(_blobToFile(_dataURItoBlob(file), _makeid() + '.jpeg')); | |
}); | |
return convertedFiles; | |
}; | |
/** | |
* @ngdoc method | |
* @methodOf OsFotos.service.service:fotosRest | |
* @name upload | |
* @description Uploads images of a work order to the server | |
* @param {Array} files An array of base64 images | |
* @param {Number} idServico The ID of work order | |
* @returns {Promise} In case of success returns a Boolean value, otherwise returns a error. | |
*/ | |
var _upload = function (files, idServico, idServicoDefinicao) { | |
var server = session.getHttpUrl() + '/uploadfotoservico', | |
contador = 1, | |
deferred = $q.defer(), | |
formData; | |
/** | |
* @name sendFile | |
* @description Send a image to server | |
* @param {String} file A base64 image | |
* @returns {Promise} Returns a promise | |
*/ | |
var sendFile = function (file) { | |
var _file = file.imagem || file; | |
idServicoDefinicao = idServicoDefinicao || file.idServicoDefinicao; | |
idServico = idServico || file.idServico; | |
if (ENV.isSimulador) { | |
var _file = file.imagem || file; | |
formData = new FormData(); | |
formData.append('file', _dataURItoBlob(_file), idServico.toString() + '_' | |
+ contador.toString() + '_' | |
+ idServicoDefinicao.toString() + '_' | |
+ file.dtFoto + '.jpg'); | |
formData.append('idServico', idServico.toString()); | |
formData.append('idServicoDefinicao', idServicoDefinicao.toString()); | |
formData.append('dtFoto', file.dtFoto.toString()); | |
return $http({ | |
method: 'POST', | |
url: server, | |
data: formData, | |
headers: { | |
'Content-Type': undefined | |
}, | |
transformRequest: angular.identity | |
}); | |
} else { | |
var options = new FileUploadOptions(); | |
options.fileKey = "file"; | |
options.fileName = idServico.toString() + '_' | |
+ contador.toString() + '_' | |
+ idServicoDefinicao.toString() + '_' | |
+ file.dtFoto + '.jpg'; | |
options.mimeType = "image/jpeg"; | |
options.params = { | |
idServico: idServico.toString(), | |
idServicoDefinicao: idServicoDefinicao.toString(), | |
dtFoto: file.dtFoto.toString() | |
}; // if we need to send parameters to the server request | |
return $cordovaFileTransfer.upload(server, _file, options); | |
} | |
}; | |
/** | |
* @name fileManager | |
* @description Manages the files to be sent | |
*/ | |
var fileManager = function () { | |
console.log('_upload.fileManager.files', files); | |
if (files && files.length > 0) { | |
sendFile(files.pop()).then(function (result) { | |
console.info('uploaded ' + (contador) + ' files of ' + idServico); | |
deferred.notify('uploaded ' + (contador) + ' files of ' + idServico); | |
contador++; | |
fileManager(); | |
}, function (err) { | |
console.error('upload failed: ', err); | |
deferred.reject(err.statusText); | |
}); | |
} else { | |
deferred.resolve(true); | |
} | |
}; | |
fileManager(); | |
return deferred.promise; | |
}; | |
return { | |
upload: _upload | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment