我在sails 中有以下文件上传脚本
uploadImage: async function (req, res) {
req.file("postImage").upload(
{
dirname: require("path").resolve(sails.config.appPath, "assets/images"),
},
function (err, uploadedFiles) {
if (err) {
return res.serverError(err);
}
if (uploadedFiles.length === 0) {
return res.badRequest("No file was uploaded");
}
var baseUrl = sails.config.custom.baseUrl;
var mediaURI = require("util").format(
"%s/assets/images/%s",
baseUrl,
uploadedFiles[0].fd
);
return res.json({
message: uploadedFiles.length + " file(s) uploaded successfully!",
mediaURI: mediaURI,
});
}
);
},
这成功上传了我的assets/images
文件夹中的图像但当我尝试在http://localhost:1337/assets/images/35820a8d-46fa-4a81-bc7b-fc2ca9d07d99.png
的浏览器中打开图像时
显示not found
我尝试了http://localhost:1337/images/35820a8d-46fa-4a81-bc7b-fc2ca9d07d99.png
,但仍然没有找到
我的保单是
module.exports.policies = {
"*": true,
};
错误是因为你在${sails.config.appPath}/assets/images
文件夹中上传了一个不同于${sails.config.appPath}/.tmp/public/images
文件夹的文件,然后你不能直接从你的URL访问资产文件夹your-domain/images/an-image.png
,为此你需要一个控制器来下载存储图像的位置:
看看这个例子,如何使用帆钩上传模块上传文件:
https://github.com/mikermcneil/ration/blob/master/api/controllers/things/upload-thing.js#L54
然后如何下载:
https://github.com/mikermcneil/ration/blob/master/api/controllers/things/download-photo.js#L50