【问题标题】:How to use an ios app to upload a image to Node.JS server如何使用 ios 应用程序将图像上传到 Node.JS 服务器
【发布时间】:2014-04-28 00:47:37
【问题描述】:

大家,我正在尝试将图像从我的 ios 设备上传到用 Node.JS 编写的网络服务器。

我在网络服务器中使用了强大的,当我通过浏览器上传文件时它运行良好,但是,我不知道如何从 IOS 应用程序上传文件。 IOS app发送的请求好像是formidable解析不出来的..

这个post(我跟着这个帖子在IOS app中写POST请求)显示了ios app客户端代码我想知道服务器端是什么样子的..

【问题讨论】:

    标签: javascript ios node.js


    【解决方案1】:

    我认为您没有提供足够的信息来获得足够的答案。但是,如果您可以使用浏览器但不能使用 iOS 发布,那么在我看来,这似乎是 POST 请求中的标头问题,或者 iOS 没有发出 POST 请求。您必须调试这些点以查看问题可能出在哪里。

    你有没有。从 iOS 发布时节点中的错误?这将揭示很多信息。

    【讨论】:

    • 感谢您的回答。我发现了问题,是因为请求体,我没有正确添加边界..
    【解决方案2】:

    好吧,我想出了如何做到这一点...简而言之,我没有以正确的格式构建请求正文。我搞砸了“边界”。在我更改了我的 IOS 应用程序代码后,我的服务器端代码实际上运行良好,this post 提供了编写 IOS 客户端代码的正确方法。 我会在这里发布我的服务器代码,以防有人需要。

        app.post('/upload', function (req, res) {
    
        var form = new formidable.IncomingForm();
        //console.log(form);
        //console.log(req._readableState);
        //console.log(req.body);    
        form.parse(req, function(err, fields, files) {
            res.end('success'); 
        });
        form.on('end', function(fields, files) {
             var temp_path = this.openedFiles[0].path;
             //console.log(temp_path);      
             var file_name = this.openedFiles[0].name;       
             var new_location = path.dirname(require.main.filename) + "/uploads/fullsize/";
             fs.copy(temp_path, new_location + file_name, function(err) {  
                if (err) {
                     console.error(err);
                } else {
                       console.log("success!")
                }
            });
        });
    });
    

    【讨论】:

      猜你喜欢
      • 2013-09-07
      • 2016-04-14
      • 1970-01-01
      • 2017-10-26
      • 2013-12-20
      • 2015-09-01
      • 2020-12-25
      • 1970-01-01
      • 2016-09-21
      相关资源
      最近更新 更多