在Sails.js后端项目中访问上传的图像

我正在尝试上传,然后访问图像。 上传工作进展顺利,将图片上传到资源/图片,但是当我尝试从浏览器中访问图片时,例如http:// localhost:1337 / images / image-name.jpg,它给了我404。 js仅用于后端目的 – 对于API,项目是使用–no-front-end选项创build的。 我的前端是AngularJS。

我的上传function:

avatarUpload: function(req, res) { req.file('avatar').upload({ // don't allow the total upload size to exceed ~10MB maxBytes: 10000000, dirname: '../../assets/images' }, function whenDone(err, uploadedFiles) { console.log(uploadedFiles); if (err) { return res.negotiate(err); } // If no files were uploaded, respond with an error. if (uploadedFiles.length === 0) { return res.badRequest('No file was uploaded'); } // Save the "fd" and the url where the avatar for a user can be accessed User .update(req.userId, { // Generate a unique URL where the avatar can be downloaded. avatarUrl: require('util').format('%s/user/avatar/%s', sails.getBaseUrl(), req.userId), // Grab the first file and use it's `fd` (file descriptor) avatarFd: uploadedFiles[0].fd }) .exec(function (err){ if (err) return res.negotiate(err); return res.ok(); }); }); } 

我看到资产/图像文件夹中的图像 – 这样 – 54cd1fc5-89e8-477d-84e4-dd5fd048abc0.jpg

http:// localhost:1337 / assets / images / 54cd1fc5-89e8-477d-84e4-dd5fd048abc0.jpg – 给404

http:// localhost:1337 / images / 54cd1fc5-89e8-477d-84e4-dd5fd048abc0.jpg – 给404

发生这种情况是因为应用程序访问的资源不是直接从assets目录访问,而是直接从项目根目录中的.tmp目录访问。

当提升帆时,资源被复制到.tmp目录,所以在提升之后添加的东西不会出现在.tmp

我通常做的是上传到.tmp并在完成后将文件复制到assets 。 这种方式的assets是没有污染的情况下上传失败的任何原因。

让我们知道这是否有效。 祝你好运!

更新find相关的链接 。