Access the uploaded image in the Sails.js project

I am trying to download and then access the image. Downloading goes well, loading the image into assets / images, but when I try to access the image from a browser, for example http: // localhost: 1337 / images / image-name.jpg it gives me 404. I use Sails.js only backend goals - for the API and the project is created with the -no-front-end option. My front end is on AngularJS.

My download function:

avatarUpload: function(req, res) { req.file('avatar').upload({ // don't allow the total upload size to exceed ~10MB maxBytes: 10000000, dirname: '../../assets/images' }, function whenDone(err, uploadedFiles) { console.log(uploadedFiles); if (err) { return res.negotiate(err); } // If no files were uploaded, respond with an error. if (uploadedFiles.length === 0) { return res.badRequest('No file was uploaded'); } // Save the "fd" and the url where the avatar for a user can be accessed User .update(req.userId, { // Generate a unique URL where the avatar can be downloaded. avatarUrl: require('util').format('%s/user/avatar/%s', sails.getBaseUrl(), req.userId), // Grab the first file and use it `fd` (file descriptor) avatarFd: uploadedFiles[0].fd }) .exec(function (err){ if (err) return res.negotiate(err); return res.ok(); }); }); } 

I see an image in the assets / images folder - something like this - 54cd1fc5-89e8-477d-84e4-dd5fd048abc0.jpg

http: // localhost: 1337 / assets / images / 54cd1fc5-89e8-477d-84e4-dd5fd048abc0.jpg - gives 404

http: // localhost: 1337 / images / 54cd1fc5-89e8-477d-84e4-dd5fd048abc0.jpg - gives 404

+5
source share
1 answer

This is due to the fact that the resources that your applications access are not directly accessible from the assets directory, but the .tmp directory in the root of the project.

Assets are copied to the .tmp directory when the sails go up, so everything added after the lift is not in .tmp .

What I usually do is upload to .tmp and copy the file to assets when done. This assets method is not polluted if the download fails for some reason.

Let us know if this works. Good luck

Update A link was found for this.

+5
source

All Articles