【发布时间】: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 的路径。 但现在我必须在一个项目中完成这两项任务。
【问题讨论】:
-
通过图片上传,我的角度服务看起来像: 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