ExpressJS从FormData获取文件 [英] ExpressJS get file from FormData

查看:39
本文介绍了ExpressJS从FormData获取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 ExpressJS 的新宠,我想使用 AJAX帖子作为 FormData对象表示js routes ,以将文件保存在服务器中.我可以从前端发布,但不能从服务器端检索数据.

以下是我尝试过的代码段:

客户端代码段中的AJAX帖子:

  var files = [];$('#upload').on('change',function(){var form_data = new FormData();form_data.append('file_uplaoded',$('#upload')[0] .files [0]);form_data.append('name1',"10000900055");form_data.append('name2',"10000900044");form_data.append('name3',"10000900033");form_data.append('name4',"10000900022");form_data.append('name5',"10000900011");console.log(form_data);$ .ajax({输入:"POST",网址:"/ulploadfile",数据:form_data,快取:false,processData:否,contentType:false,成功:功能(数据){警报('成功');}//dataType:'json',//contentType:"application/json; charset = utf-8"}).done(function(data){//alert('success1');});}); 

服务器端代码段:

  router.post('/ulploadfile',function(req,res,next){var body ='';尝试 {req.on('data',function(data){正文+ =数据;});req.on('end',function(){var post = qs.parse(body);console.log(post);res.send(上传文件响应");});}抓住(e){console.log(e);res.send(上传文件响应错误");} 最后 {}}); 

更新输出画面

所以我想知道如何从后端的请求对象中检索发布的数据.我想在不使用其他任何软件包的情况下做到这一点.

非常感谢您的帮助&建议.

解决方案

在POST请求中有一个很好的文件(多部分数据)模块,称为 multer-s3 用作存储.

  router.post('/ulploadfile',upload.single('file_uplaoded'),function(req,res,next){//req.file是`file_uplaoded`文件//req.body将保存文本字段(如果有的话)}) 

I am a new bee to ExpressJS, I want to upload a file and some data from javascript, using AJAX post as FormData object, to express js routes, to save the file in server. I am able to post from front end, but not able to retrieve data from server side.

Following is the code snippet I tried:

AJAX post from client side code snippet:

var files = [];
    $('#upload').on('change',function(){
        var form_data = new FormData();
        form_data.append('file_uplaoded',$('#upload')[0].files[0]);
        form_data.append('name1',"10000900055");
        form_data.append('name2',"10000900044");
        form_data.append('name3',"10000900033");
        form_data.append('name4',"10000900022");
        form_data.append('name5',"10000900011");
        console.log(form_data);
        $.ajax({
            type: "POST",
            url: '/ulploadfile',
            data: form_data,
            cache: false,
            processData: false,
            contentType: false,
            success: function(data){
                alert('success');
            }
            // dataType: 'json',
            // contentType: "application/json; charset=utf-8"
        }).done(function(data) {
            // alert('success1');
        });
    });

Server side code snippet:

router.post('/ulploadfile', function(req, res, next) {
    var body = '';
    try {
        req.on('data', function (data) {
            body += data;
        });
        req.on('end', function () {
            var post = qs.parse(body);
            console.log(post);
            res.send("UPLOADING FILE RESPONSE");
        });
    } catch (e) {
        console.log(e);
        res.send("UPLOADING FILE RESPONSE ERRPR");
    } finally {
    }
});

UPDATE OUT PUT SCREENSHOT

So I would like to know how retrieve the data posted from the request object in backend. I would like to do it without using any other packages.

Many Many Thanks for the help & suggestions.

解决方案

There is a good module for files (multipart data) in POST requests, called multer

You may add there validation functions, destination, different storages... There is even addon multer-s3 for that, to use as a storage.

router.post('/ulploadfile', upload.single('file_uplaoded'), function(req, res, next) {
  // req.file is the `file_uplaoded` file 
  // req.body will hold the text fields, if there were any 
})

这篇关于ExpressJS从FormData获取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆