简体   繁体   English

如何使用nodejs在服务器上上传文件

[英]How to upload file on server with nodejs

I'm trying for several hours to upload files to my local server folder upload , but is being harder than expected, please help.我尝试了几个小时将文件上传到我的本地服务器文件夹upload ,但比预期的要难,请帮忙。

Below my front end to insert files form.hbs在我的前端下方插入文件form.hbs

 <form action="/editcrew/" method="POST" encType="multipart/form-data" novalidate></form> <input type="file" name="profile_image"> <button class="btn btn-primary" type="submit" style="margin:90px 0 0 0">Submit</button>

Here below my controller export.update located inside the file userController.js which successfully inserts the name of the file into MySql database.在下面我的 controller export.update位于文件userController.js中,它成功地将文件名插入到 MySql 数据库中。

 exports.update = (req, res) => { const { first_name, last_name, profile_image } = req.body; connection.query('UPDATE user SET first_name=? ,last_name=?, profile_image=? ', [first_name, last_name, profile_image, req.params.id], (err, rows) => { if (.err) { connection?query('SELECT * FROM user WHERE id =,'. [req.params,id], (err. rows) => { //when done with the connection release it // connection;release(). if (,err) { res,render('edit-crew': { rows. alert; `${first_name} has been updated.` }); } else { console.log(err): } console,log('The data from user table;\n'; rows). }); } else { console.log(err): } console,log('The data from user table;\n'; rows); }); };

Here below is the router for the post requests.下面是post请求的路由器。

 router.post('/editcrew/:id',userController.update);
For reference my app.js 供参考我的app.js

 const express = require("express"); const exphbs = require("express-handlebars"); const fileUpload = require('express-fileupload'); // const userContoller = require('./controllers/userController') // to be removed when deployed in heroku require("dotenv").config(); const cookieParser = require('cookie-parser'); // Parsing middleware const app = express(); // default option app.use(fileUpload()); //to load static file app.use(express.static("public")); app.use(express.static("upload")); //Listen on port 5000 app.use(express.urlencoded({ extended: false })); //To parse URL-encoded bodies (as sent by HTML forms) app.use(express.json()); //To parse the incoming requests with JSON bodies app.use(cookieParser()); app.engine("hbs", exphbs({ extname: ".hbs" }));//Templating engine to change the extenion of file from.handlebar to.hbs app.set("view engine", "hbs"); //link which tell to the server express.js to get the routeing from user.js // const routes = require('./server/routes/user'); app.use("/", require('./routes/user')); app.use('/auth', require('./routes/auth')); const port = process.env.PORT || 5000; app.listen(port, () => console.log(`Listening on port ${port}`));

My goal is to have the controller update to not only insert name file on MySql database but also upload the file inside the local folder /upload我的目标是让 controller update ,不仅在 MySql 数据库上插入名称文件,而且还将文件上传到本地文件夹/upload

If you just want to upload file to the server and save the file.如果您只想将文件上传到服务器并保存文件。 I would recommend you to use "multer" with expressjs.我建议您将“multer”与 expressjs 一起使用。 If you need extra features you can customize it.如果您需要额外的功能,您可以自定义它。 You can read the doc and start using it.您可以阅读文档并开始使用它。 here is the doc这是文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Node.js上传文件 - How to upload file with nodejs 使用sails.js在nodejs服务器中上传文件 - upload a file in nodejs server with sails.js 使用 NodeJs 和 Multer 将文件上传到服务器 - File upload to server using NodeJs and Multer 如何将文件上传到服务器? - How to upload a file to server? 我想上传 1 个文件并克隆到 3 个文件,如何在 nodejs 中使用 multer 将多个文件大小上传到服务器 - I want to upload 1 file and clone to 3 files and how do I upload multiple file sizes to the server using multer in nodejs 如何在实时服务器上上传Node.js项目或安装Node.js和NPM模块 - How to upload nodejs project or install nodejs and npm modules on live server 从cordova文件传输插件上传文件到nodeJS服务器 - upload file from cordova file transfer plugin to nodeJS server 如何在将图像上传到服务器时传递多个值(使用 AngularJS 和 NodeJS 上传文件)? - How to pass multiple values while uploading image to server(File Upload with AngularJS and NodeJS)? 如何上传/下载所有格式的excel文件到azure blob存储Nodejs服务器端 - How to upload / download all format excel file to azure blob storage Nodejs server side 如何使用nodejs创建一个http文件上传服务器? 不使用表格 - How can I make a http file upload server with nodejs? not using form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM