上传pict与ASIHttpRequest.iPhone客户端/ node.js服务器



在客户端,我使用ASIHttpRequest上传图片:

// upload on server
NSLog(@"Upload to server");
UIImage *im = [UIImage imageNamed:@"ok.png"];
NSData *da = UIImageJPEGRepresentation(im, 0.6);
NSLog(@"DATA:%@", da);
NSString *n =  [NSString stringWithFormat:@"%@/upload", @"http://localhost:3000"];
NSURL *url = [NSURL URLWithString:n];
NSLog(@"URL:%@",url);
ASIFormDataRequest *r = [ASIFormDataRequest requestWithURL:url];
[r setData:da 
withFileName:@"myphoto.ico" 
andContentType:@"image/jpeg"
      forKey:@"photo"];
[r setRequestMethod:@"POST"];
[r setShouldAttemptPersistentConnection:NO];
[r startSynchronous];
NSString *resp = [r responseString];
NSLog(@"RESPONSE:%@", resp);
在服务器端,我使用node.js:
app.post('/upload', function(req, res){
    console.log(req);
    console.log(req.params);
});

客户端的请求似乎没问题,但当检查服务器端的日志时,我没有看到任何与我发送的数据相关的东西。在这种情况下,在node.js上检索数据的正确方法是什么?

您应该检查请求。文件,参见Connect的官方示例(Express内部使用):https://github.com/senchalabs/connect/blob/master/examples/bodyParser.js#L21-31

使用Connect-Form和req.form.completed就成功了。

最新更新