javascript - Upload and show images in Sails JS - Stack Overflow

With Sails JS I am trying to upload an image and show it in a view. Questions:The image is uploaded in

With Sails JS I am trying to upload an image and show it in a view.

Questions:

  • The image is uploaded in .tmp/uploads, but how can I access it from a view?
  • Is there any way to access the image?
  • The image name is changed in the directory. Is it possible not to change the name of image?

Thanks for any help you can give me.

With Sails JS I am trying to upload an image and show it in a view.

Questions:

  • The image is uploaded in .tmp/uploads, but how can I access it from a view?
  • Is there any way to access the image?
  • The image name is changed in the directory. Is it possible not to change the name of image?

Thanks for any help you can give me.

Share Improve this question edited Jan 23, 2015 at 11:26 Ruskin 6,1814 gold badges49 silver badges65 bronze badges asked Jan 23, 2015 at 10:46 Pratik HyombaPratik Hyomba 3303 silver badges14 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

1: If you want to use the image in your views you need to change the directory where it is being uploaded by passing a config object to the upload method with a dirname attribute:

req.file('image').upload({
  dirname: '../../assets/images/'
}, function(error, uploadedFiles) {
  // do something after file was uploaded...
});

Then, in your view:

<img src="/images/fileName.jpg">

// Or, if you are using Jade:
img(src='/images/fileName.jpg')

2: I'm not sure what you mean by "access the image", but the image will be saved on your local disk and can be found in .tmp/uploads/ or the directory that you pass in as dirname.

3: To keep the same name as the original file name you need to pass in a saveAs attribute to the upload method's config object:

req.file('image').upload({
  saveAs: function(file, cb) {
    cb(null, file.filename);
  }
}, function(err, uploadedFiles) {
  // do something after file was uploaded...
});

You can find more details in the Skipper documentation or Sails documentation. Skipper is the module that Sails uses for file uploads.

I have a bit of a different method of handling this since I don't automatically want to expose every file that gets uploaded.

When a file is uploaded, I leave it in a private location, and I create a record in a "Files" model that contains the fd.

I then have a route:

config/routes.js

'get  /files/:id' : 'FileControllers.stream',

Which points to my controller:

api/controllers/FilesController.js

/**
* serve a file
*
* (GET /file/:id)
*/
stream: function (req, res){

    Files.findOne(req.param('id')).exec(function (err, file){
        if (err) return res.negotiate(err);
        if (!file) return res.notFound();

        if (!file.fileDescriptor) {
            return res.notFound();
        }

        res.sendFile(file.fileDescriptor);
    });
}

res.sendFile is mentioned in the Sails documentation but is fully explained in express docs.

In short, res.sendFile:

Transfers the file at the given path. Sets the Content-Type response HTTP header field based on the filename’s extension.

By running it through a controller, I can still use my policies to dictate access.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745280730a4620265.html

相关推荐

  • javascript - Upload and show images in Sails JS - Stack Overflow

    With Sails JS I am trying to upload an image and show it in a view. Questions:The image is uploaded in

    4小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信