"Go" nodejs Tutorial--File upload based on EXPRESSJS framework

Source: Internet
Author: User

This article is a translation of an article in the original address: Handle File uploads in Express (node. js).

Uploading files early in the Nodejs development was a difficult function, followed by a formidable. It has a good head, but uploading files is still not so easy. After that, a tool Connect-form was implemented based on the original author's tutorial (tutorial on handling POST requests in Express). It makes the file upload process seem a bit simpler.

With the rapid development of the NODEJS community, it is certainly not going to take long for the functionality of uploading files to be made simple. Now this feature has been implemented-you can easily upload files in Expressjs.

Uploading files in express eliminates the need to rely on other modules, and file uploads work well within the express framework. As with Req.body, you can now use Req.files to get specific information from the files you upload.

If you do not need to specify the directory to upload the file, then you do not need to make any changes to App.js, the file will be uploaded to the '/tmp ' directory by default. But if you want to specify a specific directory for the files you upload, you just need to modify the content:

app.use(express.bodyParser());

Modified to:

app.use(express.bodyParser({uploadDir:‘./uploads‘}));

This configuration item can be found later in the V2.5.4 version of the EXPRESSJS, see Expressjs Source code, specifically on the 16th line of the Express.bodyparser. In the example above. The file will be uploaded to the uploads directory in the same directory as the Express app.

To better illustrate some of the details of file uploads during the post process, we set up a form submission page as follows:

<form Method="POST" Enctype="Multipart/form-data" Action="/file-upload"> <input Type="Text" Name= "username" > < Input type= "password" name= "password" > span class= "tag" ><input type= "file" Span class= "PLN" > name= "thumbnail" >< Span class= "PLN" > <input type= "submit" ></FORM>    

The way the server handles the above form submission is as follows:

Apppost ( '/file-upload ' , function (req, res< Span class= "pun" >, next) { Console. (req. Body Console. (req. Files});             

Let's see how easy it is!

In the example above, we can get specific information about the file we upload by calling the Req.files.thumbnail object, which depends on the definition of the form you have on the form. As shown above, the name we use is thumbnail, so we can get its information through Req.files.thumbnail.

A file contains the following commonly used property information:

    • Size----File sizes (bytes)
    • Path----file after upload
    • Name----The original filename of the file.
    • Type----File types

For the example above, file size: Req.files.thumbnail.size file Type: Req.files.thumbnail.type original file name: Req.files.thumbnail.name

Note that the default upload directory or the upload directory you specify is just a temporary directory, so you also need to move the uploaded files to the appropriate file directory. The following sample code shows how to move the uploaded file to the '/images ' directory:

   Moving files requires the use of the FS module   VarFs= Require(' FS ');App.Post('/file-upload ', function(Req,Res) { Get a temporary path to a file VarTmp_path=Req.Files.Thumbnail.Path; Specifies the directory after the file is uploaded-an example is the "Images" directory. VarTarget_path= './public/images/' +Req.Files.Thumbnail.Name; Moving filesFs.Rename(Tmp_path,Target_path, function(Err) { If (Err) ThrowErr; Delete the Temp folder file,Fs.Unlink(Tmp_path, function() { If (Err  throw Err;. Send ( ' File uploaded to: '  +  Target_path +  '-'  + Req.. Thumbnail. Size +  ' bytes ' );   });  };             

You can specify the upload directory as any directory that you can access in the current operating system environment. If you want these files to be publicly accessible, you can copy the files to the application's static directory '/public '. Note that if you need to copy the file to public or its subdirectories, make sure that the folder exists and can be written to. Otherwise there will be ' error:enoent, no such file ' and other exception information.

Uploading files in Expressjs now is a very simple thing to do. If your original app needs to use file uploads, then I recommend that you upgrade your app to the latest EXPRESSJS framework and modify your program to ensure that the functionality works correctly.

from:https://cnodejs.org/topic/4f40a4dc0feaaa4424081758

"Go" nodejs Tutorial--File upload based on EXPRESSJS framework

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.