Home > Web Front-end > JS Tutorial > body text

Code example for multiparty to implement file upload in nodejs

不言
Release: 2018-08-13 16:11:14
Original
2099 people have browsed it

The content of this article is about the code example of multiparty implementation of file upload in nodejs. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

var multiparty = require('multiparty');
function fileUpload(req,res)
    {
        //生成multiparty对象,并配置上传目标路径
        var form = new multiparty.Form({uploadDir: '../static/images/'});
        //上传完成后处理
        form.parse(req, function(err, fields, files) {
        var filesTmp = JSON.stringify(files,null,2);
        if(err){
            } else {
            var inputFile = files.file[0];
            var uploadedPath = inputFile.path;
            var dstPath = '../static/images/' + inputFile.originalFilename;
             //重命名为真实文件名
            fs.rename(uploadedPath, dstPath, function(err) {
            if(err){
                    console.log('rename error: ' + err);
                    } else {
                    console.log('rename ok');
                    }
                });
            }
            res.send({"fileName":inputFile.originalFilename});
        });
    }
 route.post('/fileUpload',(req,res)=>{
        fileUpload(req,res);
    });
Copy after login

Code source: https://github.com/chengyaxin/vue-test/blob/master/server/api.js

This is the code I wrote about the vue example , if you are interested, you can take a look at this example!

vue example renderings:

Related recommendations:

How to use js The setInterval timer method implements the carousel chart (complete code)

Encapsulates get requests and post requests in the mini program into global functions (code)

The above is the detailed content of Code example for multiparty to implement file upload in nodejs. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!