从Angular端发送文件后,在Node.js端对大文件进行编码时出错 [英] Error on encoding large file on Node.js side after sending file from Angular-side

查看:92
本文介绍了从Angular端发送文件后,在Node.js端对大文件进行编码时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在服务器端将文件获取为console.log(body).

I am getting the file on server side as console.log(body).

{ fieldname: 'image',
  originalname: '21329726_1866651723650020_188839340_o.jpg',
  encoding: '7bit',
  mimetype: 'image/jpeg',
  destination: './pics/',
  filename: '5146c9818ff517c426e34ad84ff3513f',
  path: 'pics/5146c9818ff517c426e34ad84ff3513f',
  size: 94093 
}

,然后通过const base64Data = body.buffer.toString("base64")进行编码.

但是对于小尺寸文件,它可以很好地工作,但是对于大文件,它会造成问题.编码不正确.

But for small size files, it works well but for large files it creates a problem. It is not encoding properly.

我认为问题是由于此原因,它在接收到完整文件之前就开始编码.

I think the problem is due to, it starts encoding before it receives the full file.

请给我一些正确的方法.

Please give me some proper way to do this.

这是我的GitHub链接此处.

This is my GitHub link here.

推荐答案

在您的/server/routes/user.js

update this code in your /server/routes/user.js

const express = require('express');
const router = express.Router();
// const mongoose = require('mongoose');
const path = require('path');
const multer = require('multer');

var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, __basedir + '/upload');
  },
  filename: function (req, file, cb) {
    cb(null, file.originalname)
  }
})

const upload = multer({
  storage: storage,
  limits: {
    maxFileSize: 1024 * 1024 * 5
  }
}).single('filename');

router.post('/upload', upload, (req, res) => {
  res.send('uploaded');
});

router.post('/submit', (req, res) => {
  console.log(req.body);
});

module.exports = router;

还要在app.js中添加以下行

also add below line in app.js

global.__basedir=__dirname;

这篇关于从Angular端发送文件后,在Node.js端对大文件进行编码时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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