Start a new topic

Uploading a public readable Image/File

Hey,



i am trying to upload a public (:: All users of our application) readable images (profil images)



Problem: i've tried several things, but the images are not saved as public files





Code used:

var promise = Kinvey.File.upload(fileBlob, {

// _id : 'my-file-id',

// _filename : 'my-file.txt',

mimeType : 'image/png',

size : fileBlob.length,

public : true

}, {

public : true,

success: function(file) {

_map['avatar'] = {

_type : 'KinveyFile',

_id : file._id

};

// Remove item from User Map

delete _map['profil_image'];



// Update User with ProfilImage

updateUser(_map);

},

error : function(error){

console.log("error uploading File");

}

});





The double of "public:true" was just a test, but nothing of both methods worked ...



Anything i've missed?



Best,

Nico

That would work, yes, but in addition I recommend setting the global readable flag (`_acl.gr`) to `true` when uploading the file to the Kinvey backend. That way, getting the file by `file-id` will also work for *all* users.
Thus i have to save the downloadurl (public available) at the moment, the user uploaded it's image/file to the Kinvey-Backend?





So that any other user can download it by it's downloadurl and not by the file-id which results in an error?
The `public: true` should be set in the options (third parameter). To explain what this flag does, it is important to distinguish between the file itself (`fileBlob`), and the entity created in the Kinvey backend.



What this means is that when you login as a different user, `Kinvey.File.download('my-file-id')` will result in an Authorization error. This is because the entity created in Kinvey is not readable to this other user (unless you modify the `_acl` of course).



The direct link to the file (`fileBlob`) is however public. So the approach here would be to upload the file, then obtain the download URL. This download URL will be publicly available:



```

Kinvey.File.upload(fileBlob, { _id: 'my-file-id' }, { public: true }).then(function(file) {

Kinvey.File.stream('my-file-id').then(function(response) {

var downloadURL = response._downloadURL;

});

});

```



The `downloadURL` variable in the snippet above is the public file. This URL will be valid forever, and accessible by whoever.
Login or Signup to post a comment