【问题标题】:Uploading image to server via node.js from an iOS app从 iOS 应用程序通过 node.js 将图像上传到服务器
【发布时间】:2013-09-07 19:26:12
【问题描述】:

我正在开发一个 iOS 应用程序,我正在使用 node.js 进行服务器端脚本。我在从 iOS 应用程序上传图像到服务器时遇到问题。如果我从网页表单上传图像,它工作正常。但如果从应用端上传,则无法正常工作。

 //test file
 h3 Pic Upload
 form(action='/pic_upload', method='post',enctype='multipart/form-data') 
  | user_pic:
  input(type='file', name='user_pic')
  input(type='submit')

 //app.js
 var userlogin =require('./routes/userlogin');
 app.post('/pic_upload', userlogin.picUpload);

//userlogin.js
//picUpload function
exports.picUpload = function(req, res) {
console.log(req.files);  // showing undefined, when called from IOS app
// pic upload script...
});

我尝试从应用程序端发送图像作为数据或文件参数,但没有成功。如何从应用端发送文件参数,以便我可以轻松地将图像上传到服务器?请提出解决问题的方法。

【问题讨论】:

  • 你能显示你的客户端代码吗?

标签: ios objective-c node.js file-upload


【解决方案1】:

此代码适用于我的应用程序。试试这个。

NSData *imgData = UIImageJPEGRepresentation(newImg, 0.2);

NSString *str = @"displayImage";

    NSString *urlString = @"http://some.url.com/post";

    // create request
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
    [request setHTTPShouldHandleCookies:NO];
    [request setTimeoutInterval:30];
    [request setURL:[NSURL URLWithString:urlString]];

    [request setHTTPMethod:@"POST"];

    NSString *boundary = [NSString stringWithFormat:@"---------------------------14737809831464368775746641449"];

    // set Content-Type in HTTP header
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
    [request setValue:contentType forHTTPHeaderField: @"Content-Type"];

    // post body
    NSMutableData *body = [NSMutableData data];

    // add params (all params are strings)
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"currentEventID\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[@"52344457901000006" dataUsingEncoding:NSUTF8StringEncoding]];


    // add image data
    if (imgData) {
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
       // [body appendData:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"displayImage\"; filename=\"image.jpg\"\r\n"]];
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", str] dataUsingEncoding:NSUTF8StringEncoding]];

        [body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:imgData];
        [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    }

    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    // setting the body of the post to the reqeust
    [request setHTTPBody:body];

    // set URL
    [request setURL:[NSURL URLWithString:urlString]];

    NSString *bodyStr = [[NSString alloc]initWithData:body encoding:NSUTF8StringEncoding];
    NSLog(@" %@",bodyStr);

    NSURLConnection *lot_Connection=[[NSURLConnection alloc]initWithRequest:request delegate:self];

    if(lot_Connection)
    {
        webdata = [[NSMutableData alloc] init];

    }

【讨论】:

  • 你如何从你的 nodejs 服务器获取这个图像?来自“request.body”还是“request.file”?可以举个例子吗
猜你喜欢
  • 1970-01-01
  • 2017-06-09
  • 2016-04-14
  • 2012-02-29
  • 2013-12-20
  • 1970-01-01
  • 1970-01-01
  • 2017-10-26
  • 2014-11-25
相关资源
最近更新 更多