首页 文章

如何将图像从 iOS App 上传到 node.js?

提问于
浏览
-1

iOS 代码,我想用使用 multiparty 将图像从 ipad app 上传到节点 js 服务器

服务器是 Node.js&express,upload.js 路径在./routes 中。我希望使用多方。但是 node-multiparty/examples/upload.js 并不表达。谁能帮我?

1 回答

  • 0

    代码与示例中给出的代码非常相似。你只需将 form.parse 放在路线中,就像在这个例子中一样

    var path = require('path');
        var express = require('express');
        var multiparty = require('multiparty');
        var util = require('util');
    
        var app = express();
    
        /*var staticPath = path.resolve(__dirname, '/public');
        app.use(express.static(staticPath));*/
    
        //Simulating web server
        app.get('/uploadImage', function (req, res) {
            res.writeHead(200, {'content-type': 'text/html'});
            res.end(
                '<form action="/uploadImage" 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>'
            );
        })
    
        //This routes matters
        app.post('/uploadImage', function (req, res) {
    
            // parse a file upload
            var form = new multiparty.Form();
    
            //After your request body is parsed with multipart/form encoding
            //You can access to the files and the fields in that form
            //You may upload it to another image service
            form.parse(req, function (err, fields, files) {
              res.writeHead(200, {'content-type': 'text/plain'});
              res.write('received upload:\n\n');
    
              //The property that matter inside files object it's path
              //With that, you can write it in your server file system (fs) 
              //or data base or cloud service
              console.log('files received', files);
              console.log('fields received', fields);
              res.end(util.inspect({fields: fields, files: files}));
            });
    
        })
    
        app.listen(3000, function() {
          console.log('listening');
        });
    

    确保安装所有依赖项 npm install express multiparty

相关问题