Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement simple File upload function by nodejs

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you the "nodejs how to achieve simple file upload function", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "nodejs how to achieve simple file upload function" this article.

First of all, we need to take a look at the directory structure, and then start our little demo.

There are three ways to upload files:

1. The file upload function is realized through third-party plug-ins such as flash,activeX.

two。 Through the html form tag to achieve file upload function, advantages: good browser compatibility.

3. Through the asynchronous request of xhr level2, you can use Baidu formData object.

Use 2 to do an exercise here.

For the node plug-in, please take a look at the package.json file

{"name": "upload", "version": "0.1.0", "description": "upload demo", "main": "app.js", "scripts": {"test": "echo\" Error: no test specified\ "& & exit 1"}, "author": "iwang", "license": "ISC", "dependencies": {"body-parser": "^ 1.15.2" "connect-multiparty": "^ 2.0.0", "express": "^ 4.14.0"}}

Dependencies is the dependent plug-in for this demo. The node.js I use is version 4.4.4, you can replace the above code into your package.json file, and execute npm install to automatically install the three dependent plug-ins needed by demo.

App.js

/ * Created by iwang on 2017-1-15. * / / express uses @ 4 version. Var express = require ('express'); / / middleware required by the form form. Var mutipart= require ('connect-multiparty'); var mutipartMiddeware = mutipart (); var app = express (); / / the storage location of temporary files will be modified below. If it is not stored elsewhere by default, it is not described in detail here. It took me three or four hours to find this method in Baidu to modify the storage location of temporary files. I have to say that nodejs is really difficult to learn. / / so leave my study record here for later reading. App.use (mutipart ({uploadDir:'./linshi'})); / / sets the port number that the http service listens to. App.set ('port',process.env.PORT | | 3000); app.listen (app.get (' port'), function () {console.log ("Express started on http://localhost:"+app.get('port')+'; press Ctrl-C to terminate.');}); / / A html file app.get ('/', function (req,res) {res.type ('text/html') is output when the browser accesses localhost Res.sendfile ('public/index.html') / / this is for play, written by the routing function of the express framework, and has nothing to do with uploading files. App.get ('/ about',function (req,res) {res.type ('text/plain'); res.send (' Travel about'); / / this is the interface path to accept form form requests using post. App.post ('/ upload',mutipartMiddeware,function (req,res) {/ / print here you can see the information of the received file. Console.log (req.files); / * / / do something * successfully received the file from the browser. We can write a series of operations on the file here. For example, rename, modify the file storage path. Wait. * return a success prompt to the browser. Res.send ('upload customers')

Public/index.js

Title

Attachment:

Browser address to access index.html

Select file: I selected a timg.jpg file

Click submit and jump to the page, prompting you to upload successfully

Let's take a look at whether the price plus the name of linshi already exists in the file we uploaded. The following image proof file has been stored under upload_demo/linshi, and the name of the picture has been changed to a temporary name.

We can write code in app.js to process our picture files.

Main points of demo:

1. First of all, install nodejs. My version here is 4.4.4.

two。 Create a file named in English. Create a package.json file manually or using npm init. Replace the contents of the above package.json file with the contents of the package.json file you created.

3. Use npm install to install plug-ins in package.json. The node_modules file is generated automatically. Our dependent plug-ins are all here.

4. Write our app.js index.html file. Or copy the above two codes.

5. The command line executes node app.js.

6. Our simple upload page will appear when the browser visits localhost:3000.

The above is all the content of this article "how to achieve simple file upload function in nodejs". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report