简体   繁体   中英

Images are uploaded but not visible in UI => sails js

Images are got uploaded in the assets folder but it's not visible in the UI. When I restart the server then I could see the image.

Problem: Images are only getting stored in the assets but has not stored in the .tmp file.

When restarting server the .tmp reads all the files which are in the assets folder, but it is not happening while uploading image.

This is the situation which I am facing.

This is the code i tried to solve it with:

var file = req.file('logo'); 
file.upload({ dirname: '../../assets/images' }, function onUploadComplete (err, files) { 
    if (err) { 
        console.log("something went wrong") ;
    } else { 
        console.log("File has uploaded successfully");
    }
});

The reason you are seeing the file after the server is restarted is because of the grunt task copying the assets into the .tmp folder. So there are a few things you may want to do to fix your issue:

  1. Copy the file to the .tmp folder yourself in the onUploadComplete callback.
  2. Set the grunt to monitor for changes, but I think this is not a good practice for production.
  3. On upload move your file from the server to some other repository service (I use S3 service of AWS for example). By the way, I think this is the best choice since you decouple your static assets from uploaded content - if you ever need to redeploy the server, you may loose the uploaded images.
  4. You can always save images as a base64 encoded strings in a DB, so no issues with paths, file system operations, etc., but then you are putting a lot of load on your DB.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM