试图在MongoDB中上传保存

发布于 2025-02-04 02:55:29 字数 1475 浏览 1 评论 0原文

我目前有以下代码,该代码将临时文件保存到public/files我试图了解mongodb gridfs文档,但没有成功。

我想知道如何将文件保存在mongodb gridfs中而不是我的public/file目录中,

我知道我缺少需要将上传的文件发送到mongodb的部分 - 这是我不知道的部分做。

在Mongodb示例中,他们说要做类似的事情:

fs.createReadStream('./myFile').pipe(
  bucket.openUploadStream('myFile', {
    chunkSizeBytes: 1048576,
    metadata: { field: 'myField', value: 'myValue' },
  })
);

但是我不使用FS或需要将文件上传到温度,然后进行fs

import formidable from 'formidable';
import { MongoClient, ObjectId } from 'mongodb';
var Grid = require('gridfs-stream');

export const config = {
  api: {
    bodyParser: false,
  },
};

export default async (req, res) => {
  const uri = process.env.MONGODB_URI;
  let client;
  let clientPromise;
  const options = {};
  client = new MongoClient(uri, options);
  clientPromise = client.connect();
  const clients = await clientPromise;
  const database = clients.db('AdStitchr');
  var gfs = Grid(database, client);
  gfs.collection('uploads');

  const form = new formidable.IncomingForm();

  form.uploadDir = 'public/files';
  form.keepExtensions = true;
  form.parse(req, (err, fields, files) => {
    var file = files.file;
    console.log(JSON.stringify(file));

    try {
      const newFile = File.create({
        name: `files\${file.newFilename}.mp3`,
      });
      res.status(200).json({ status: 'success' });
    } catch (error) {
      res.send(error);
    }
  });
};

I currently have the following code, which saves the temp file to public/files I have tried to understand the MongoDB GridFS documentation but with no success.

I am wondering how do I get the files to save inside MongoDB GridFS instead of my public/file directory

I am aware I am missing the part where I need to send the uploaded file to mongodb - this is the part I don't know how to do.

In mongodb example they say to do something like:

fs.createReadStream('./myFile').pipe(
  bucket.openUploadStream('myFile', {
    chunkSizeBytes: 1048576,
    metadata: { field: 'myField', value: 'myValue' },
  })
);

however I am not using FS or do I need to upload the file to the temp and then do the fs

import formidable from 'formidable';
import { MongoClient, ObjectId } from 'mongodb';
var Grid = require('gridfs-stream');

export const config = {
  api: {
    bodyParser: false,
  },
};

export default async (req, res) => {
  const uri = process.env.MONGODB_URI;
  let client;
  let clientPromise;
  const options = {};
  client = new MongoClient(uri, options);
  clientPromise = client.connect();
  const clients = await clientPromise;
  const database = clients.db('AdStitchr');
  var gfs = Grid(database, client);
  gfs.collection('uploads');

  const form = new formidable.IncomingForm();

  form.uploadDir = 'public/files';
  form.keepExtensions = true;
  form.parse(req, (err, fields, files) => {
    var file = files.file;
    console.log(JSON.stringify(file));

    try {
      const newFile = File.create({
        name: `files\${file.newFilename}.mp3`,
      });
      res.status(200).json({ status: 'success' });
    } catch (error) {
      res.send(error);
    }
  });
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文