gpt4 book ai didi

javascript - 在 Sails JS 中上传和显示图像

转载 作者:搜寻专家 更新时间:2023-11-01 00:32:04 24 4
gpt4 key购买 nike

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com