天道酬勤,学无止境

multer

如何在NodeJS中将多部分请求从一台服务器传递到另一台服务器?(How to pass multipart request from one server to another in NodeJS?)

问题 我有两个 nodeJS 服务器,服务器 1 从客户端获取请求并将其传递给服务器 2,服务器 2 向服务器 1 返回响应并响应客户端。 客户端上传一个文件,它必须以与我拥有的任何其他休息请求相同的方式传递。 我用服务器1 Axios公司将数据发送到服务器2和multer在服务器2存储在磁盘上的文件。 我将请求从 server1 发送到 server2 时遇到问题,因为请求的正文不包含任何内容并且请求是Multipart 。 我应该如何处理服务器 1 上的请求??? router.post('/fileUpload', (req, res) => { console.log(req.body); res.status(200).json({ msg: "Got file" }); }); 回答1 您可以使用form-data模块从 nodejs 应用程序发送多部分/表单数据 这是您可以在 Server1 上实现的代码,用于从客户端接收图像文件并将其发送到 Server2。 const express = require("express"); const app = express(); const bodyParser = require('body-parser'); var multer = require('multer')(); const FormData =

2021-12-03 13:31:21    分类:技术分享    node.js   axios   multipartform-data   multer

如何将multer保存在内存中的文件上传到另一个API(how to upload file saved in memory by multer to another API)

问题 我有2 个单独的NodeJS API ,它们使用 multer 将文件保存在内存中。 我的快递中间件看起来像这样 import multer from 'multer'; const storage = multer.memoryStorage(); export default multer({ storage }).single('image'); 我能够成功接收保存在内存中的文件,所以我的req.file.image看起来像这样 { fieldname: 'image', originalname: 'image-2017-08-28-13-47-31-218.png', encoding: '7bit', mimetype: 'image/png', buffer: <Buffer 89 50 4e 47 0d 0a 1a 0 ... >, size: 493181 } 收到文件后,在第一个API 上,我需要将其发送到同样使用multer & express的第二个 API function secondApiReceiveImage(req, res) { console.log(req.file) console.log(req.files) console.log(req.body) res.send('ok'); } 我尝试使用以下实现发送 通过 https

2021-12-02 09:38:46    分类:技术分享    node.js   express   file-upload   multer

如何将多部分/表单数据从 Angular 发布到 Nodejs Multer?(How to post multipart/form-data from Angular to Nodejs Multer?)

问题 从 Angular 我想将图像作为 Blob 数据上传到 nodeJS 服务器。 服务器在后端使用multer 。 图像文件由画布渲染生成。 我从服务器收到以下错误: 错误:多部分:未找到边界状态:500 以下是我的代码。 请帮我找出问题所在。 角度: // blob:Blob; -> it has valid image data. var formData: FormData = new FormData(); formData.append('banner', blob, "my-file.png") this.http.post(url, formData, { headers: new Headers({ 'Content-Type': 'multipart/form-data' }) }) .toPromise() .then(res => { console.log(res); return res.json(); }) .catch(this.handleError); 节点: router.post('/upload-banner-image', bannerImageUpload.single('banner'), watchfaceController.uploadWatchfaceBannerImage); 回答1 删除您的'Content-Type'

2021-12-01 12:54:44    分类:技术分享    node.js   angular   multipartform-data   multer

使用 mongoose、gridfs-stream 和 multer 上传文件时出现 MongoError(MongoError when uploading a file using mongoose, gridfs-stream and multer)

问题 我正在使用 multer、gridfs-stream 和 mongoose 和 mongodb 运行 express 4,我正在尝试上传文件并将其流式传输到 gridfs。 执行此操作的快速路由定义为: app.post('/uploadfile', function (req, res) { console.dir(req.files); // The mongodb instance created when the mongoose.connection is opened var db = mongoose.connection.db; // The native mongo driver which is used by mongoose var mongoDriver = mongoose.mongo; // Create a gridfs-stream var gfs = new Gridfs(db, mongoDriver); var file = req.files.myFile; var fileId = new ObjectId(); console.log("Creating WriteStream"); var writeStream = gfs.createWriteStream({ _id: fileId, filename: file

2021-11-28 23:58:05    分类:技术分享    node.js   mongoose   node-mongodb-native   gridfs-stream   multer

Change filename when using express/multer

I'm having trouble uploading a pdf, when I upload it (I'm using ng-file-upload) the file json that reachs express.js is: { fieldname: 'file', originalname: 'db.pdf', encoding: '7bit', mimetype: 'application/pdf', destination: './client/public/docs/transactions/', filename: '8a3fe7cb7369d9cffa8e17b162ec3d6b', path: 'client\\public\\docs\\transactions\\8a3fe7cb7369d9cffa8e17b162ec3d6b', size: 9179145 } I'd like to change the name to a property that I send from angular to express, here's all the setup: html element: <div ngf-select="vm.uploadPdf($file, $errFiles)" ngf-pattern="'.pdf'" accept=

2021-11-28 23:22:12    分类:问答    node.js   express   multer

Nodejs Express 4 Multer | Stop file upload if user not authorized

I'm using multer as multipart middelware for express4. Express is configured to use passport as auth middelware, but I cannot find a way to prevent file upload if the user is not authenticated. I thought to use onFileUploadStart to reject the file, but I cannot find a link with "request" object, with which it would be possible to match the user. Below code use in configuring express vs multer: ... // Multipart file upload app.use(multer( { dest: wwwroot + path.sep + 'uploaded' + path.sep, onFileUploadStart: function (file) { //TODO : apply security check : user auth, file size, number

2021-11-28 19:48:02    分类:问答    node.js   express   multer

Error: invalid json with multer and body-parser

I am currently using multer for multipart/form-data in node.js application alongside with body-parser. I tried to POST form-data using POSTMAN, but it is getting this error. Error: invalid json at parse (/Users/k/Documents/application/node_modules/body-parser/lib/types/json.js:79:15) at /Users/k/Documents/application/node_modules/body-parser/lib/read.js:102:18 at IncomingMessage.onEnd (/Users/k/Documents/application/node_modules/body-parser/node_modules/raw-body/index.js:136:7) at IncomingMessage.g (events.js:199:16) at IncomingMessage.emit (events.js:104:17) at _stream_readable.js:908:16 at

2021-11-28 07:33:25    分类:问答    json   node.js   form-data   body-parser   multer

Renaming an uploaded file using Multer doesn't work (Express.js)

I'm trying to upload a file from a HTML form using Express.js and Multer. I've managed to save the file to the desired location (a folder named uploads). However, I'd like to rename the file while uploading it because, by default, Multer gives it a strange name such as: 5257ee6b035926ca99923297c224a1bb Might be a hexadecimal time stamp or so but I need a more explicit name in order to call a script on it later. I've followed the explanation found here but it doesn't do anything more than it used to: uploading the file with the hexa name. Also, the two events onFileUploadStart and

2021-11-28 07:03:35    分类:问答    javascript   node.js   express   upload   multer

Node/Multer Get Filename

I am using the following to upload files to a directory via Multer. It works great, but I need to perform some actions after upload that require the name of the file I just posted to the "upload" directory. How do I get the name of the file I just posted? // Multer storage options var storage = multer.diskStorage({ destination: function(req, file, cb) { cb(null, 'upload/'); }, filename: function(req, file, cb) { cb(null, file.originalname + '-' + Date.now() + '.pdf'); } }); var upload = multer({ storage: storage }); app.post('/multer', upload.single('file'), function(req, res) { // Need full

2021-11-27 17:47:12    分类:问答    node.js   multer

How can we send data in MySQL when the form has set to enctype="multipart/form-data" in node.js?

This enctype="multipart/form-data" blocking me from sending my data to mysql but it is a must for multer to work. I'm trying to store the image filename to database while the file will store to my project directory ./uploads using multer framework. I've tried different queries but nothing happen. I'm hoping for you guys which have faced the same issue like mine please take a little of your time. Thank you in advance. <form class="add-music-form" action="/save" method="POST" enctype="multipart/form-data"> <div class="form-group"> <div class="song-feaured-image"> <input style="display: none;"

2021-11-26 16:15:53    分类:问答    mysql   node.js   express   multer