【问题标题】:How to save text data and save image in a server folder using angular, nodejs and mongodb如何使用 Angular、nodejs 和 mongodb 将文本数据和图像保存在服务器文件夹中
【发布时间】:2018-11-26 15:03:00
【问题描述】:

如何将文本数据保存到 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 的路径。 但现在我必须在一个项目中完成这两项任务。

【问题讨论】:

  • 使用npm busboy
  • 通过图片上传,我的角度服务看起来像: let data = { "stotra": godObject.stotra, "yantra": godObject.yantra, "incarnation": godObject.incarnation, "family": godObject.family,“apprearance”:godObject.apprearance,“place”:godObject.place,“subincarnation”:godObject.subincarnation,“aarti”:godObject.aarti,};返回 this.httpClient.post(ADDGOD_URL, data, httpOptions);现在,当我必须保存数据和图像时,我必须改变什么?感谢@Ashish 的帮助。
  • @Ashish,我已经更新了我的问题。

标签: node.js angular mongodb file-upload


【解决方案1】:

我已经使用 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

    // construct a model
    let godObj = new godModel(name, othername, epithet);

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

【讨论】:

    猜你喜欢
    • 2015-12-21
    • 1970-01-01
    • 1970-01-01
    • 2014-12-11
    • 1970-01-01
    • 2017-07-01
    • 2020-03-03
    • 2018-12-26
    • 2015-10-10
    相关资源
    最近更新 更多