Node.js   发布时间:2022-04-24  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了node.js – 在Sails.js后端项目中访问上传的图像大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试上传然后访问图像.上传进展顺利,将图片上传到资源/图片,但是当我尝试从浏览器访问图像时,如 http://localhost:1337/images/image-name.jpg,它给了我404.我只将Sails.js用于后端目的 – 对于API,项目是用–no-front-end选项.我的前端是在AngularJS上.

我的上传功能

avatarUpload: function(req,res) {
    req.file('avatar').upload({
        // don't allow the @R_545_10586@l 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();
            });
    });
}

我在assets / images文件夹中看到了这个图像 – 就像这样 – 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和@L_874_21@文件.这样,如果上载因任何原因失败,资产不会受到污染.

如果有效,请告诉我们.祝好运!

更新
找到了相关的link.

大佬总结

以上是大佬教程为你收集整理的node.js – 在Sails.js后端项目中访问上传的图像全部内容,希望文章能够帮你解决node.js – 在Sails.js后端项目中访问上传的图像所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。