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 Node uploads files

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Editor to share with you how to upload Node files, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

First of all, we can give an initial solution, and then continue to improve in the code. The first plan I have given is a simple logic:

Well, first of all, let's complete the first step: check the picture parameters.

I wrote a method here to check whether the image parameter is complete. The parameter requires a startup diagram plus four commodity attribute values. If the parameter does not exist, it will directly return error to the client.

The second step is to implement the logic of uploading images, so I defined a general method of uploading files:

UploadPicsAndCheckPar (req, checkParFunc, maxPic, pathDir, isNeedUid, cb)

Let's first take a look at how the parameters are understood:

Req: request from form checkParFunc: method to detect uploaded file parameters maxPic: maximum number of uploaded images limit pathDir: server saves uploaded file folder name isNeedUid: assign a uidcb to each picture: callback to the routing layer

First, configure the upload file home directory in config.js:

CONFIG.PICSMAINPATH ='/ mnt/test'

Next, generate the multiparty object and configure the upload target path:

Var form = new multiparty.Form ({uploadDir: (mainPath +'/ picTemp/')})

Then perform the image upload operation:

Form.parse (req,function (error,fields,files))

Is it over when you upload the picture here? Certainly not! Don't forget what we just said to limit the number and size of pictures. First, let's parse the parameter format:

Check whether the number of uploaded images is greater than the maxPic we set. If it exceeds the number, delete the uploaded images and return error. Check whether the size of the picture is more than 4m, and if it exceeds 4m, delete the picture and return error.

We can take a look at the logic of deleting a picture, which is actually a loop form to delete a picture:

Next, we have to determine whether there is only one name for each picture. If there are two names in an image, delete the image and return error:

Of course, since we are going to package the uploaded file into a public method, we certainly have to adapt to the case of non-image files. I have dealt with the video here:

If the video size meets the requirements, verify that the parameters meet the API requirements, and rename the video name if it meets the API requirements. Let's take a look at how the renaming method is implemented:

First of all, we need to construct the path. I use the current year, month, day and uid as the path to traverse to see if the path exists. If it does not exist, create a folder.

Finally, update the image path to the real path.

Come here to verify that the image parameters are all passed, and the picture is uploaded successfully. Returns a list of parameters to the routing layer. The logical processing of the database layer is carried out in the routing layer.

We will complete a complete upload function here. Next, let's test whether the downlink upload interface is working properly.

You can see that if the parameter is incomplete, the corresponding error message is returned directly to the client.

If you pass parameters according to the API requirements we just set, you can see that our pictures have been uploaded successfully. Next, we can see if there are any pictures uploaded successfully in the upload folder we defined.

You can see that the image was uploaded successfully, and the path is to use the current year, month and day as the folder. We can quickly view the pictures we have uploaded. Of course, if you need to upload a lot of resources, uploading all the resources to the server will put a certain load on the server, so we can actually upload the pictures directly to COS. COS is actually very convenient to use, the official SDK can be directly introduced to use, these are not covered in this article.

The above is all the contents of the article "how to upload files in Node". 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

Internet Technology

Wechat

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

12
Report