Camilo Casadiego Camilo Casadiego - 1 year ago 81
Javascript Question

ngFileUpload on safari posts empty file

Im using ngFileUpload to send images to cloudinary service, my app is build on ionic and is intended to run on ios and android, I have the following code to upload images:

.service('photoUploadService', ['$q','Upload', '$ionicLoading', 'AppModel',
function($q, $upload, $ionicLoading, AppModel){
// creating a return object with a data property, matching the structure we return from the router resolve
var ref = this;

ref.dataURItoBlob = function(dataURI) {
var contentType = 'image/png';
var sliceSize = 512;

var byteCharacters = atob(dataURI.split(',')[1]);
var byteArrays = [];

for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
var slice = byteCharacters.slice(offset, offset + sliceSize);

var byteNumbers = new Array(slice.length);
for (var i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);

var byteArray = new Uint8Array(byteNumbers);


var blob = new Blob(byteArrays, {type: contentType});
return blob;

ref.uploadPhoto = function(imageData, waitMessage, sucessCallback, sucessCBParameters){
var deferred = $q.defer();

var filename = Math.floor(Math.random()*90000) + 10000 + ".png";
//var imageBase64 =',')[1];
var blob = ref.dataURItoBlob(imageData);//new Blob([imageBase64], {type: 'image/png'});
var file = new File([blob], filename, {type: 'image/png'});

file.upload = $upload.upload({
url: "",
data: {
upload_preset: "xxxxx",
tags: 'myphotoalbum',
context: 'photo=' + filename,
file: file
}).progress(function (e) {
file.progress = Math.round((e.loaded * 100.0) /;
file.status = "Uploading... " + file.progress + "%";
${template: waitMessage + (file.progress) + "%"});
}).success(function (data, status, headers, config) { = data;

}).error(function (data, status, headers, config) {
file.result = data;
console.error('PENDING ERROR HANDLER');
return deferred.promise;

When I run my code under android, it works flawlessly and the images the uploaded to the server, but running on IOs I get an "Empty File" error back from cloudinary. Debugging the code, the file seems to be created correctly and the only error I'm getting is "Empty file".

So far I haven't been able to identify were the actual problem is, if it's with ngFileUpload or the way I'm creation the image fiel.

So far I've tried the steps described in this posts looking at a poss Convert base64 data url to image file in angularjs Convert base64 data image file in angularjs and with no luck.

Any help would be appreciated.

Answer Source

For anyone else with this problem, ng-file-upload cannot be used along with cordova, here the original answer ng-file-upload not uploading in Ionics

In case someone else needs the code to easily upload pictures to cloudinary, this is the one that is working for me.

ref.uploadPhoto = function(imageData, album, waitMessage, imgType){
    var deferred = $q.defer();
    var filename = Math.floor(Math.random()*90000) + 10000 + ".png";
    var options = {
        fileName: filename,
        chunkedMode: false,
        mimeType: "image/png",
        params: {'upload_preset': "XXXXX",
                'tags': album,
                'context': 'photo=' + filename},


        imageData, options)
            var response = JSON.parse(result.response);
            response.type = imgType;
        }, function(err){
        }, function(progress){
            var val = Math.round(((progress.loaded * 100.0) /;
                ${template: waitMessage + val + "%"});    

    return deferred.promise;