multer

How to limit the file size when uploading with multer?

扶醉桌前 提交于 2019-12-03 11:24:21
问题 I'm making a simple file upload system with multer: var maxSize = 1 * 1000 * 1000; var storage = multer.diskStorage({ destination: function (req, file, callback) { callback(null, 'public/upload'); }, filename: function (req, file, callback) { callback(null, file.originalname); }, onFileUploadStart: function(file, req, res){ if(req.files.file.length > maxSize) { return false; } } }); var upload = multer({ storage : storage}).single('bestand'); router.post('/upload',function(req,res){ upload

Node.js and Multer - Handle the destination of the uploaded file in callback function (req,res)

两盒软妹~` 提交于 2019-12-03 11:04:44
问题 i'm a newbie in Node.js and i have run into a very simple problem lately. I'm using a module named multer, so users can upload images. In my web app all the users have a unique id, and i want the uploaded images to be stored in a directory named based on their id. Example: .public/uploads/3454367856437534 Here is a part of my routes.js file: // load multer to handle image uploads var multer = require('multer'); var saveDir = multer({ dest: './public/uploads/' + req.user._id, //error, we can

Uploading multiple files with multer, but from different fields?

主宰稳场 提交于 2019-12-03 07:17:39
How can I have multer accept files from multiple file type fields? I have the following code that uploads a single file, using multer in node.js: var storage = multer.diskStorage({ destination: function (req, file, callback) { callback(null, './public/uploads'); }, filename: function (req, file, callback) { callback(null, file.fieldname + '-' + Date.now()); } }); var upload = multer({ storage : storage }); app.post('/rest/upload', upload.array('video', 1), function(req, res, next){ ... } From the following form, on the condition only the video field has a value (if I specify both I get an

Uploading large file to NodeJS webserver

陌路散爱 提交于 2019-12-03 07:09:40
I am serving up my web application using NodeJS server (ExpressJS) currently. One of the new requirements is for the users to be able to upload large videos (potentially in gigs) to the server. They will later then be able to download them again. Could I achieve this with the current stack? I came across tutorials using Multer JS with "multipart/form-data" specified by the front-end. If I use this, would my Express be able to serve up other HTTP requests while writing a giant single video file to the server's disk? Multer should work fine. It'll stream the data as it comes in over HTTP to a

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

依然范特西╮ 提交于 2019-12-03 06:49:58
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...

Node JS AWS S3 file upload. How to get public URL response

旧时模样 提交于 2019-12-03 05:46:21
问题 I'm uploading a file to Amazon S3 using the Node SDK. The file uploads are working fine, but I want to get the public url of the file to send back to the client. At the moment the response I get is: Successfully uploaded data { ETag: '"957cd1a335adf5b4000a5101ec1f52bf"' } Here is my code. I'm using a Node Express server, and Multer to handle uploads. app.use(multer({ // https://github.com/expressjs/multer dest: './public/uploads/', limits : { fileSize:100000 }, rename: function (fieldname,

Error: invalid json with multer and body-parser

落花浮王杯 提交于 2019-12-03 03:35:19
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

How to limit the file size when uploading with multer?

*爱你&永不变心* 提交于 2019-12-03 01:49:35
I'm making a simple file upload system with multer: var maxSize = 1 * 1000 * 1000; var storage = multer.diskStorage({ destination: function (req, file, callback) { callback(null, 'public/upload'); }, filename: function (req, file, callback) { callback(null, file.originalname); }, onFileUploadStart: function(file, req, res){ if(req.files.file.length > maxSize) { return false; } } }); var upload = multer({ storage : storage}).single('bestand'); router.post('/upload',function(req,res){ upload(req,res,function(err) { if(err) { return res.end("Error uploading file."); } console.log(req.file); res

Reading contents of csv file in node.js

£可爱£侵袭症+ 提交于 2019-12-02 23:33:22
I am trying to implement a module in nodejs( just started working in nodejs ) which has requirement below as Upload .csv file. Read content of the csv file. Frameworks currently being used for restful api is "express": "~4.2.0" and multer for file upload. Now I have configured multer like below in my app.js app.use(multer({ onFileUploadData : function(file, data){ console.log('onFileUploadData Called with data - '+ data); } })); In my route file, I have a post endpoint like below app.post('/sample.csv',lead.processCSV); This route is being called from an ajax call below as $.ajax({ xhrFields:

node js multer file upload not working. req.file and req.files always undefined

戏子无情 提交于 2019-12-02 18:11:35
问题 I am trying to upload a file to my server, but req.file and req.files is always undefined on my POST REST endpoint. The content I'm trying to upload is a ".dat" file, and I am expecting a json response. Here's my code: Server side: var express = require('express'); var multer = require('multer') var upload = multer({ dest: 'uploads/' }) var app = express() app.post('/creoUpload', upload.single('uploadFileField'), function(req, res, next) { console.log("req.file = ",JSON.stringify(req.file));