简体   繁体   English

如何使用iOS应用将图像上传到Node.JS服务器

[英]How to use an ios app to upload a image to Node.JS server

everyone, I'm trying to upload a image from my ios device to a web server which is written in Node.JS. 所有人,我正在尝试将ios设备中的图像上传到用Node.JS编写的Web服务器。

I use formidable in the web server and it works well when I upload a file via browser, however, I can't figure out how to upload a file from an IOS app. 我在网络服务器中使用了可怕的功能,当我通过浏览器上传文件时,效果很好,但是,我不知道如何从IOS应用程序上传文件。 It seems that the request sent by IOS app can't be parsed by formidable.. 看来IOS应用发送的请求无法被强大的解析。

This post (I followed this post to write the POST request in IOS app) shows the ios app client code I wonder what does the server side look like.. 这篇文章 (我跟随这篇文章在IOS应用中编写POST请求)显示了ios应用客户端代码,我想知道服务器端是什么样的。

I don't think you have provided enough information to receive an adequate answer. 我认为您没有提供足够的信息来获得适当的答案。 However, if you can post fine using a browser but not with iOS, to me that seems like either a header issue in the POST request, or iOS is not issuing a POST request. 但是,如果您可以使用浏览器而不是iOS进行发布,则对我而言,似乎是POST请求中出现标题问题,或者iOS未发出POST请求。 You will have to debug those points to see where the problem may be. 您将不得不调试这些点以查看问题所在。

Are you getting any. 你有东西吗 errors in node when posting from iOS? 从iOS发布时节点中出现错误? That will reveal a lot of information. 那将显示很多信息。

Well, I figured out how to do this...in short, I wasn't building the request body in proper format.. I messed up with 'boundary'. 好吧,我想出了办法...总之,我没有以正确的格式构建请求主体。.我搞砸了“边界”。 My server side code actually works well after I changed my IOS app code, this post is giving the correct way to write IOS client code. 更改IOS应用程序代码后,我的服务器端代码实际上运行良好, 本文为编写IOS客户端代码提供了正确的方法。 I will post my server code here in case someone may need it. 如果有人需要,我将在此处发布我的服务器代码。

    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!")
            }
        });
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM