如何将图像上传到cloudinary

时间:2017-10-04 04:57:39

标签: javascript node.js cloudinary

我想将图像上传到我的NodeJS api的cloudinary形式,我有我的模型,在这个模型中我有一个名为image的字段,这个字段是字符串类型,这里我想保存一个接收的url作为响应cloudinary。

这是我的model.js

'use strict';

const mongoose = require('mongoose');

const CursoSchema = mongoose.Schema({
  name: {
    type: String,
    required: true
  },
  image: {
    type: String,
  },
  description: {
    type: String,
    required: true
  }
});

module.exports = mongoose.model('Curso', CursoSchema);

我的controller.js,这是我必须保存网址的地方,我尝试了使用cloudinary文档的方法。

var imageFile = req.files.image;
cloudinary.uploader.upload(imageFile.path, function(result){
  if (result.url) { res.status(200).send({url: result.url});
  }else {
    console.log('Error uploading to cloudinary');
 }
});

但我刚刚上传了图片。这是我的controller.js

'use strict';

const Curso = require('../models/curso');
const config = require('../config');
const cloudinary = require('cloudinary');

cloudinary.config({
  cloud_name: 'something',
  api_key: 'somthingelse',
  api_secret: 'andotherthing'
})

function saveCurso(req, res) {
  let curso = new Curso();
  let params = req.body;
  curso.name =  params.name;
  curso.description = params.description;
  //How to get the image and upload it to cloudinary

  curso.save((err, cursoSaved)=>{
    if(err){return res.status(500).send({message:'Error'});}
    if(!cursoSaved) return res.status(404).send({message: 'Empty'});
    return res.status(200).send({curso: cursoSaved});
  });
}

module.exports = {
  saveCurso,
}

我的routes.js文件:

'use strict';

const express = require('express');
const api = express.Router();
const cursoController = require('../controllers/curso');

api.post('/curso', cursoController.saveCurso);

module.exports =  api;

我想用名称,描述和图像保存数据,我想从cloudinary中保存网址。

我在cloudinary中保存了一些图像,但我找不到在图像字段中仅保存网址的方法

注意:我正在使用正文解析器,这是我的app.js

'use strict';

const bodyParser = require('body-parser');
const express = require('express');
const app = express();

const curso_routes = require('./routes/curso');

app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());

app.use('/api', curso_routes);

module.exports = app;

1 个答案:

答案 0 :(得分:1)

首先,您需要使用multermultiparty这样的包来处理multipart/form-data。所以,使用multiparty:

'use strict';

const Curso = require('../models/curso');
const config = require('../config');
const cloudinary = require('cloudinary');
const multiparty = require('multiparty');

cloudinary.config({
  cloud_name: 'something',
  api_key: 'somthingelse',
  api_secret: 'andotherthing'
})

function saveCurso(req, res) {

  //How to get the image and upload it to cloudinary
  let form = new multiparty.Form({ maxFilesSize: 10 * 1024 * 1024 }); //setting max size of image to 10MB
  form.parse(req, (err, fields, files) => {
    if (err) return err
    let curso = new Curso();
    curso.name = fields.name;
    curso.description = fields.description;
    cloudinary.v2.uploader.upload(files.content[0].path, (err, result) => { // content is the name of the image input on the front end form
      if (err) return err
      curso.image = result.secure_url;
      curso.save((err, cursoSaved) => {
        if (err) { return res.status(500).send({ message: 'Error' }); }
        if (!cursoSaved) return res.status(404).send({ message: 'Empty' });
        return res.status(200).send({ curso: cursoSaved });
      });
    });
  }) 
}