与 Sails JS我正在尝试上传图像并在 View 中显示它。
问题:
- 图像已上传到 .tmp/uploads 中,但如何从 View 中访问它?
- 有什么方法可以访问该图像吗?
- 目录中的图像名称已更改。是否可以不更改图像名称?
感谢您给我的任何帮助。
最佳答案
1:如果您想在 View 中使用图像,您需要通过将配置对象传递到 upload
来更改上传图像的目录。方法 dirname
属性:
req.file('image').upload({
dirname: '../../assets/images/'
}, function(error, uploadedFiles) {
// do something after file was uploaded...
});
那么,在你看来:
<img src="/images/fileName.jpg">
// Or, if you are using Jade:
img(src='/images/fileName.jpg')
2:我不确定您所说的“访问图像”是什么意思,但图像将保存在您的本地磁盘上,并且可以在.tmp/uploads/
中找到或您传入的目录 dirname
.
3:要与原始文件名保持相同的名称,您需要传入 saveAs
属性为upload
方法的配置对象:
req.file('image').upload({
saveAs: function(file, cb) {
cb(null, file.filename);
}
}, function(err, uploadedFiles) {
// do something after file was uploaded...
});
您可以在 Skipper documentation 中找到更多详细信息或Sails documentation 。 Skipper 是 Sails 用于文件上传的模块。
关于javascript - 在 Sails JS 中上传并显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28108173/