存储使用猫鼬一个Multer文件到MongoDB的 [英] Storing a file into MongoDB using Multer in Mongoose

查看:196
本文介绍了存储使用猫鼬一个Multer文件到MongoDB的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经得到了这个远来接受一个文件在我的HTML表单,并通过 $ http.post 使用 NG-与棱角分明的后文件上传模块。现在我要接受这个猫鼬文件并将其保存到我的NoSQL MongoDB中托管于MongoLab。
我看了一下这个模块名为 Multer 和遵循的基本文件,但只有我远。在我解释这个问题的开始让我后我的code:

I have gotten this far to accept a file in my HTML form and post in with angular via an $http.post using the ng-file-upload module. Now I want to accept this file in Mongoose and store it into my NoSQL MongoDB hosted on MongoLab. I have read about this module called Multer and followed the basic documentation, but it only me as far. Before I explain the beginning of the problem let me post my Code:

我的HTML表单:

<form name="upForm">
   <fieldset>
      <legend>Upload files here</legend>
         <label>Insert File Here:</label>
         <input type="file" ngf-select ng-model="exFile" name="file" ngf-accept="'.cs'" required>
         <i ng-show="upForm.file.$error.required">*required</i>
         <div class="alert" ng-show="file.$error === 'pattern'">
              file type is not accepted
         </div>
         <br />
         <button ng-disabled="!upForm.$valid" ng-click="uploadExercise(exFile)" class="btn btn-default">Submit</button>
         <span class="progress" ng-show="picFile.progress >= 0">
         <div style="width:{{exFile.progress}}%" ng-bind="picFile.progress + '%'"></div>
         </span>
         <span ng-show="picFile.result">Upload Successful</span>
    </fieldset>
</form>


我的角code:


My Angular Code:

 $scope.uploadExercise = function (file) {

    console.log(file);
    var fd = new FormData();
    fd.append('file', file);
    $http.post(url+"/Upload", fd,{
        transformRequest: angular.identity,
        header:{'Content-Type': undefined},
        enctype:'multipart/form-data'
    }).success(function () { }).error(function () { });
    console.log(fd);
};

控制台日志返回正确的文件对象。

console logs return the correct file objects.

猫鼬至今:

var mongoose = require("mongoose");
var express = require("express");
var multer = require('multer');
var upload = multer({ dest: 'Uploads/' });
var bodyparser = require("body-parser");
var app = express();


mongoose.connect("connection-string");
app.use(bodyparser.json());

app.post('/Upload', upload.single('solution') ,function (req, res, next) {
    console.log(req.file);
});

的console.log 一直返回undefined。所以什么东西,就走可怕的错误。请帮助我!
我希望收到该文件在我的猫鼬,并将其存储到MongoDB中,我从来没有这样做之前,似乎无法找到Multer或任何体面的文件存储文件是有关我的情况什么像样的解释。我究竟做错了什么?我应该怎么做呢?

This console.log keeps returning undefined. So something, somewhere went terribly wrong. Please help me out! I want to receive this file in my Mongoose and store it into the MongoDB, I have never done this before and can't seem to find any decent documentation for Multer or any decent explanation for storing files that is relevant for my case. What am I doing wrong? What should I be doing instead?

推荐答案

你是在错误的道路上跑步的人。我在Multer直接用于保存文件的磁盘系统文件,而不是你的数据库的previous问题的请求已经解释。对于必须使用GridFS的。

Man you are running on the wrong path. I have already explained in your previous question request that Multer is used to save files in file disk system and not to your database directly. For that you must use GRIDFS.

即将到当前的问题。

app.post('/Upload', upload.single('solution') ,function (req, res, next) {
    console.log(req.file);
});

下面的 upload.single('解决方案') - 调用一个函数上传,并通过文件名是解决方案,但它已相当明显,它不是可以在这里。

Here the upload.single('solution') - calls a function Upload and the file name passed is solution but it is obvious enough that it isn't available here.

使用这种类型的格式 - Multer的文档

use this type of format - documentation of Multer

var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, '/tmp/my-uploads')
  },
  filename: function (req, file, cb) {
    cb(null, file.fieldname + '-' + Date.now())
  }
})

var upload = multer({ storage: storage })

储存部分有用来给路径,您的文件必须保存和文件名部分用于更改,你想拥有的文件名。

The Storage Part there is used to give path to where your file must be saved and the file name section is used to make changes to the file name that you would like to have.

请阅读文档,因为这会帮助你。当我们使用第三方模块,我们必须承认,他们已经给了,使我们可以轻松地使用他们的工作的信息。

Please read the documentation because that'll help. When we use third party modules we must acknowledge the information they have already given so that we can use their work easily.

让我打你更容易。这里是现成的code的作品。

Let me make it easier for you. Here is ready made code that works.

<一个href=\"http://stackoverflow.com/questions/32895968/multer-throwing-weird-error-while-uploading-file-via-ng-file-upload\">Multer扔奇怪的错误,同时通过NG-文件上传文件上传

去检查该线程。有人提出由我 - 这个问题有我在阵列格式的多个文件,一次发送文件,。如果你不这样做,只是改变NG-文件上传段使用单次上传演示的例子和服务器端的NodeJS code替换 .array 。单,事情会工作,你希望他们工作的方式 - 因为你要使用的文件系统的磁盘来存储文件

Go check that thread. The question was raised by me - the problem there was I was sending files in array format, as in multiple files at once. If you are not doing that just change ng-file-upload segment to use the single upload demo example and on server side nodejs code replace .array with .singleand things will work the way you want them to work - given that you want to use file disk system to store files.

我再说一遍,这种方法不会帮助您将文件直接保存在MongoDB中。

I repeat that this method wont help you to save the file in mongodb directly.

让我知道如果你需要任何进一步的澄清。

Let me know if you need any further clarification.

这篇关于存储使用猫鼬一个Multer文件到MongoDB的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆