search

Home  >  Q&A  >  body text

node.js - react native通过image-picker选择了图片并通过fetch上传,后端nodejs怎么接?

这是reactnative的代码

    uploadImage(imageuri){
        let formData = new FormData();
        let file = {uri: imageuri,type:'multipart/form-data',name:'image.png'};
        formData.append('files',file);
        fetch('http://127.0.0.1:8080/image',{
            method:'POST',
            headers:{
                'Content-Type':'multipart/form-data',
            },
            body:fromData,
        })
            .then((response)=>response.text())
            .then((responseData)=>{
                    console.log('responseData',responseData);
            })
            .catch((error)=>{console.error('error',error)});
    }

后端 express

app.post('/image',function(req,res){后面不知道如何处理,才能保存到数据库或者保存到本地
PHP中文网PHP中文网2837 days ago774

reply all(2)I'll reply

  • 阿神

    阿神2017-04-17 16:26:11

    https://github.com/expressjs/...

    https://cnodejs.org/topic/564...

    You can refer to the link above and use multer

    Files are generally not saved to the database

    reply
    0
  • 迷茫

    迷茫2017-04-17 16:26:11

    Backend express
    Print req.body and see if req.body.files has a value. This value is an object that contains information about the file you uploaded, file name, size, etc., then extract it and save it to the file you want to save. folder.
    formidable middleware, the bottom layer of express is implemented using it. Here are the official examples.

    var formidable = require('formidable'),
        http = require('http'),
        util = require('util');
    
    http.createServer(function(req, res) {
      if (req.url == '/upload' && req.method.toLowerCase() == 'post') {
        // parse a file upload
        var form = new formidable.IncomingForm();
    
        form.parse(req, function(err, fields, files) {
          res.writeHead(200, {'content-type': 'text/plain'});
          res.write('received upload:\n\n');
          res.end(util.inspect({fields: fields, files: files}));
        });
    
        return;
      }
    
      // show a file upload form
      res.writeHead(200, {'content-type': 'text/html'});
      res.end(
        '<form action="/upload" enctype="multipart/form-data" method="post">'+
        '<input type="text" name="title"><br>'+
        '<input type="file" name="upload" multiple="multiple"><br>'+
        '<input type="submit" value="Upload">'+
        '</form>'
      );
    }).listen(8080);
    

    soonfy

    reply
    0
  • Cancelreply