【问题标题】:Upload pict with ASIHttpRequest. iPhone client / node.js server使用 ASIHttpRequest 上传图片。 iPhone 客户端/node.js 服务器
【发布时间】:2011-12-26 18:03:04
【问题描述】:

在客户端我使用 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 上检索数据的正确方法是什么?

【问题讨论】:

    标签: node.js asihttprequest express


    【解决方案1】:

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

    【讨论】:

    • 您使用的是什么版本的 Express?
    • 我已经升级到 espress 2.5.1。 req.files.length 给出 0 并且 req.files.image 未定义。
    • 我在 iPhone 本地编程方面是 0,所以.. 你确定你正确地发送了图片..?
    • 你可能是对的,但老实说,根据文档,这似乎没问题。
    • 好的,使用 node-formidable 的基本示例(由 express 在后台使用)进行测试:github.com/felixge/node-formidable/tree/master/example
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-11
      • 2015-10-30
      • 1970-01-01
      • 2012-02-19
      • 1970-01-01
      • 2011-11-18
      • 2019-03-05
      相关资源
      最近更新 更多