【问题标题】:How to upload image to node.js from iOS App?如何从 iOS 应用程序将图像上传到 node.js?
【发布时间】:2016-04-14 23:05:31
【问题描述】:

iOS代码,我想用upload image from ipad app to node js server using multiparty

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

【问题讨论】:

  • 听起来你是在要求别人为你做这件事,这不会让你走得太远。您是否尝试过自己并遇到特定问题?如果是这样,请就该问题提出问题。

标签: ios node.js express image-uploading


【解决方案1】:

代码与示例中给出的代码非常相似。你只需要把 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 多方

【讨论】:

  • 我已将其重命名为upload.js,命令:node upload.js。如何从 iOS 客户端调用它?图像将保存在哪里?你能给我一个完整的项目吗(iOS & Node.js)。非常感谢。
  • 在给出的示例中,iOS 客户端发送如下内容: Content-Type: multipart/form-data; boundary=---------------9051914041544843365972754266 Content-Length: 554 此消息通过路由 /uploadImage 发送到 nodejs 服务器,带有post 方法和一些带有一些内容类型标题的数据。在回调中有一个名为 files 的参数。请阅读 cmets。
猜你喜欢
  • 2013-09-07
  • 1970-01-01
  • 2013-08-27
  • 1970-01-01
  • 1970-01-01
  • 2012-02-29
  • 1970-01-01
  • 1970-01-01
  • 2016-01-06
相关资源
最近更新 更多