Sails.js 上传图片无效

Sails.js upload image is not work

我正在尝试上传图片并将图片名称输入 MySQL。但我仍然坚持上传图片。我总是得到结果 "files(s) uploaded successfully"

这是控制器。

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 连接添加到配置目录中的 connections.js 文件。
  2. 您已经生成了一个名为 'gallery' 的模型和控制器,它在您的 MySQL 数据库中代表一个 table,也称为 'gallery',并且您的模型已成功创建为此 table.
  3. 中字段的表示
  4. 在画廊 table/model 中,您有一个名为 'image_name' 的字段用于存储图像的名称,还有一个名为 'image_uid' 的字段用于存储唯一标识符(文件描述符) .

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

/**
 * Gallery.js
 *
 * @description :: TODO: You might write a short summary of how this model works and what it represents here.
 * @docs        :: http://sailsjs.org/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 中,创建一个上传 function/route 来处理图像上传和数据库插入。这应该看起来像:

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 %>" >