如何使用angular,nodejs和mongodb保存文本数据并将图像保存在服务器文件夹中

时间:2018-06-17 15:31:22

标签: node.js angular mongodb file-upload

如何将文本数据保存到mongodb数据库并将图像存储到服务器文件夹。 我使用nodejs作为服务器端语言,使用angular作为客户端,使用mongodb作为数据库。

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { ADDGOD_URL } from '../utils/urls';
import { httpOptions } from '../utils/httpheader';
import { GodModel } from '../../model/godmodel';

@Injectable()
export class GodService {
imageFile: File;
constructor(private httpClient: HttpClient) { }

addGod(godObject: GodModel): Observable<any> {
let fileList: FileList = godObject.imageEvent.target.files;
if (fileList.length > 0) {
    this.imageFile = fileList[0];
}

let data = 
{
"name": godObject.name,
"othername": godObject.othername,
"epithet": godObject.epithet,
"gender": godObject.gender,
"weapon": godObject.weapon,
"instrument": godObject.instrument,
"vehicle": godObject.vehicle,
"mantra": godObject.mantra,
"stotra": godObject.stotra,
"yantra": godObject.yantra,
"incarnation": godObject.incarnation,
"family": godObject.family,
"apprearance": godObject.apprearance,
"place": godObject.place,
"subincarnation": godObject.subincarnation,
"aarti": godObject.aarti,
"story": godObject.story,
"knownfor": godObject.knownfor,
"poojaitem": godObject.poojaitem,
"books": godObject.books,
"dosdonts": godObject.dosdonts,
"festival": godObject.festival,
"ritual": godObject.ritual,
"abode": godObject.abode,
"type": godObject.type,
"form": godObject.form,
"auspicioustime": godObject.auspicioustime,
"image": this.imageFile
};
return this.httpClient.post<any>(ADDGOD_URL, data, httpOptions);
}
}

我已经制作了一个示例代码,用于单独保存纯文本数据和一个不同的代码,用于将图像保存到文件夹中,并保存到mongodb的路径。 但现在我必须在一个项目中完成这两项任务。

1 个答案:

答案 0 :(得分:0)

我已经使用Multer在服务器文件夹上上传图像并将其名称存储到数据库中。

这是我的代码,
storage = multer.diskStorage({
    destination: function (req, file, uploadFn) {
        uploadFn(null, path.join(__dirname, '../public/uploads/'));
    },
    filename: function (req, file, cb) {
        if (file) {
            var isimage = isImage(file.originalname);
            if (isimage) {
                cb(null, file.originalname)
            } else {
                return;
            }
        }
    }
});

var upload = multer({
    storage: storage
});

我必须上传多张图片,因此请使用upload.arrayconst

route.post('/addgod', upload.array('images', 5), (req, res) => {
    // get all fields
    var name = req.body.name;
    var othername = req.body.othername;
    var epithet = req.body.epithet
    var gender = req.body.godgender;
    var weapon = req.body.weapon;
    var instrument = req.body.instrument;
    var vehicle = req.body.vehicle;
    var mantra = req.body.mantra;
    var stotra = req.body.stotra;
    var yantra = req.body.yantra;
    var incarnation = req.body.incarnation;
    var family = req.body.family;
    var apprearance = req.body.apprearance;
    var place = JSON.parse(req.body.place);
    var subincarnation = req.body.subincarnation;
    var aarti = req.body.aarti;
    var story = req.body.story;
    var knownfor = req.body.knownfor;
    var poojaitem = req.body.poojaitem;
    var books = req.body.books;
    var dosdonts = req.body.dosdonts;
    var festival = req.body.festival;
    var ritual = req.body.ritual;
    var abode = req.body.abode;
    var type = req.body.type;
    var form = req.body.form;
    var auspicioustime = req.body.auspicioustime;
    var associated_gods = req.body.associatedgods;
    var literature = req.body.literature;
    var isPublish = false;

    // construct a model
    let godObj = new godModel(name, othername, epithet, gender, weapon, instrument, vehicle, mantra, stotra, yantra, incarnation, family, apprearance, place, subincarnation, aarti, story, knownfor, poojaitem, books, dosdonts, festival, ritual, abode, type, form, auspicioustime, imageName, category1, category2, category3, category4, articleTitle, associated_gods, literature, isPublish);

    const godcrud = require('../db/god/godcrud');
    godcrud.addGodDetails(godObj, res); // call a function to save data on database
});