最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Sails.js上传图片不起作用

运维笔记admin8浏览0评论

Sails.js上传图片不起作用

Sails.js上传图片不起作用

我正在尝试上传图像并将图像名称输入MySQL。但我仍然坚持上传图片。我总是得到结果“文件上传成功”

这是控制器。

upload: function(req, res){
    var picture_path = req.param('picture_path');

    req.file('image').upload({maxBytes: 1000000, dirname : '/assets/pic_items'},function whenDone(err, uploadedFiled){
        if (err) {
            return res.negotiate(err);
        }   
            console.log(uploadedFiled);
            return res.json({
                message: uploadedFiled.length + ' files(s) uploaded successfully'
            });
        if (uploadedFiled.length === 0){
            return res.badRequest('no file was uploaded');
        }
    });
},
回答如下:

为了回答你的问题,我将假设一些事情。

  1. 您已成功将MySQL连接添加到config目录中的connections.js文件中。
  2. 您已经生成了一个名为“gallery”的模型和控制器,它代表了MySQL数据库中也称为“gallery”的表,并且您的模型已成功创建为此表中字段的表示形式。
  3. 在图库表/模型中,您有一个名为“image_name”的字段用于存储图像的名称,还有一个名为“image_uid”的字段,用于存储唯一标识符(文件描述符)。

所以现在你应该有一个看起来像这样的Gallery模型:

/**
 * Gallery.js
 *
 * @description :: TODO: You might write a short summary of how this model works and what it represents here.
 * @docs        :: http://sailsjs/documentation/concepts/models-and-orm/models
 */

module.exports = {

  attributes: {
    // Anything else you want to capture in your DB
    image_name : {
      type : 'string'
    },

    image_uid : {
      type: 'string'
    },
  }
};

在GalleryController中,创建上传函数/路由以处理图像上载和数据库插入。这应该是这样的:

upload: function(req, res, next) {
    var params = req.params.all();
    console.log(params);

    req.file('fileToUpload').upload({
        // don't allow the total upload size to exceed ~10MB
        dirname: '../../assets/images/gallery',
        maxBytes: 10000000
    },function (err, uploadedFile) {
        if (err) {
            return res.serverError(err); 
        }

        // If no files were uploaded, respond with an error.
        if (uploadedFile.length === 0){
            return res.serverError("No files were uploaded!"); 
        }

        // Use this log all the uploaded file info
        // console.log(uploadedFile[0]);

        // Get the name of the file
        var fileName = uploadedFile[0].filename;
        // Get the file descriptor and remove the directory details
        var fileUID = uploadedFile[0].fd.replace(/^.*[\\\/]/, '');

        // Create a galleryItem to insert into database
        var galleryItem = {};
        galleryItem.image_name = fileName;
        galleryItem.image_uid = fileUID;

        // Create the image in your Database
        Gallery.create(galleryItem, function (err, gallery) {
            if(err) {
                return res.serverError('An error occured while adding Image in the DB');
            }

            // return whatever or wherever you want
            return res.redirect("/gallery/");
        });
    });
},

最后,在客户端上,您应该确保表单捕获使用多部分编码,输入名称与控制器中的'req.file(“fileToUpload”)'参数匹配。这是一个非常基本的例子:

<form action="/gallery/upload" method="post" enctype="multipart/form-data">
  <input type="file" name="fileToUpload" id="fileToUpload">
  <input type="submit" value="Upload Image" name="submit">
</form>

显示图像就像从数据库中读取库项目并将image_uid传递给图像标记一样简单。

<img src="/images/gallery/<%= gallery.image_uid %>" alt="<%= gallery.image_name %>" >

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论