Javascript 如何将图像上载到cloudinary
我想从我的NodeJS api上传一个图像到cloudinary,我有我的模型,在这个模型中我有一个名为image的字段,这个字段是string类型,这里我想保存接收作为cloudinary响应的url 这是我的model.jsJavascript 如何将图像上载到cloudinary,javascript,node.js,cloudinary,Javascript,Node.js,Cloudinary,我想从我的NodeJS api上传一个图像到cloudinary,我有我的模型,在这个模型中我有一个名为image的字段,这个字段是string类型,这里我想保存接收作为cloudinary响应的url 这是我的model.js 'use strict'; const mongoose = require('mongoose'); const CursoSchema = mongoose.Schema({ name: { type: String, required: t
'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,这里是我必须保存url的地方,我尝试了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,
}
和my 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保存url
我在cloudinary中保存了一些图像,但是我找不到在我的图像字段中只保存url的方法
注意:我使用的是body解析器,这是我的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;
首先,您需要使用类似或的包来处理
多部分/表单数据。因此,使用多方:
'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 });
});
});
})
}
您向/api/curso
发出post请求,正文为表单数据
,并选择其中一个输入作为文件而不是文本。根据上面的例子,关键是“内容”,值是图像。事实上,我做了,但我只得到了错误,我记录了它,这是我的另一个领域,需要你解释更多吗?你犯了什么错误?您记录了什么?这个:if(err){console.log(err);return res.status(500)。send({message:'Error'});}要在postman中获得响应并在控制台和我的控制台中查看错误,我看到:ValidationError:Curso验证失败:name:Pathname
是必需的。,描述:路径description
是必需的。当您尝试使用postman时,您是发送“名称”和“描述”字段还是仅发送图像?