admin管理员组文章数量:1418134
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 badges2 Answers
Reset to default 41: 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.
本文标签: javascriptUpload and show images in Sails JSStack Overflow
版权声明:本文标题:javascript - Upload and show images in Sails JS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745280730a2651410.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论